| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 * Handles the Heartbeat API in the no-privilege context via AJAX. 16 * 17 * Runs when the user is not logged in. 18 * 19 * @since 3.6.0 20 */ 21 function wp_ajax_nopriv_heartbeat() { 22 $response = array(); 23 24 // 'screen_id' is the same as $current_screen->id and the JS global 'pagenow'. 25 if ( ! empty( $_POST['screen_id'] ) ) { 26 $screen_id = sanitize_key( $_POST['screen_id'] ); 27 } else { 28 $screen_id = 'front'; 29 } 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 $response The no-priv Heartbeat response. 40 * @param array $data The $_POST data sent. 41 * @param string $screen_id The screen ID. 42 */ 43 $response = apply_filters( 'heartbeat_nopriv_received', $response, $data, $screen_id ); 44 } 45 46 /** 47 * Filters Heartbeat Ajax response in no-privilege environments when no data is passed. 48 * 49 * @since 3.6.0 50 * 51 * @param array $response The no-priv Heartbeat response. 52 * @param string $screen_id The screen ID. 53 */ 54 $response = apply_filters( 'heartbeat_nopriv_send', $response, $screen_id ); 55 56 /** 57 * Fires when Heartbeat ticks in no-privilege environments. 58 * 59 * Allows the transport to be easily replaced with long-polling. 60 * 61 * @since 3.6.0 62 * 63 * @param array $response The no-priv Heartbeat response. 64 * @param string $screen_id The screen ID. 65 */ 66 do_action( 'heartbeat_nopriv_tick', $response, $screen_id ); 67 68 // Send the current time according to the server. 69 $response['server_time'] = time(); 70 71 wp_send_json( $response ); 72 } 73 74 // 75 // GET-based Ajax handlers. 76 // 77 78 /** 79 * Handles fetching a list table via AJAX. 80 * 81 * @since 3.1.0 82 * 83 * @return never 84 */ 85 function wp_ajax_fetch_list() { 86 $list_class = $_GET['list_args']['class']; 87 check_ajax_referer( "fetch-list-$list_class", '_ajax_fetch_list_nonce' ); 88 89 $wp_list_table = _get_list_table( $list_class, array( 'screen' => $_GET['list_args']['screen']['id'] ) ); 90 if ( ! $wp_list_table ) { 91 wp_die( 0 ); 92 } 93 94 if ( ! $wp_list_table->ajax_user_can() ) { 95 wp_die( -1 ); 96 } 97 98 $wp_list_table->ajax_response(); 99 100 wp_die( 0 ); // @phpstan-ignore deadCode.unreachable (The wp_list_table_class_name filter can supply a class whose ajax_response() returns.) 101 } 102 103 /** 104 * Handles tag search via AJAX. 105 * 106 * @since 3.1.0 107 */ 108 function wp_ajax_ajax_tag_search() { 109 if ( ! isset( $_GET['tax'] ) ) { 110 wp_die( 0 ); 111 } 112 113 $taxonomy = sanitize_key( $_GET['tax'] ); 114 $taxonomy_object = get_taxonomy( $taxonomy ); 115 116 if ( ! $taxonomy_object ) { 117 wp_die( 0 ); 118 } 119 120 if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) { 121 wp_die( -1 ); 122 } 123 124 $search = wp_unslash( $_GET['q'] ); 125 126 $comma = _x( ',', 'tag delimiter' ); 127 if ( ',' !== $comma ) { 128 $search = str_replace( $comma, ',', $search ); 129 } 130 131 if ( str_contains( $search, ',' ) ) { 132 $search = explode( ',', $search ); 133 $search = array_last( $search ); 134 } 135 136 $search = trim( $search ); 137 138 /** 139 * Filters the minimum number of characters required to fire a tag search via Ajax. 140 * 141 * @since 4.0.0 142 * 143 * @param int $characters The minimum number of characters required. Default 2. 144 * @param WP_Taxonomy $taxonomy_object The taxonomy object. 145 * @param string $search The search term. 146 */ 147 $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $taxonomy_object, $search ); 148 149 /* 150 * Require $term_search_min_chars chars for matching (default: 2) 151 * ensure it's a non-negative, non-zero integer. 152 */ 153 if ( ( 0 === $term_search_min_chars ) || ( strlen( $search ) < $term_search_min_chars ) ) { 154 wp_die(); 155 } 156 157 $results = get_terms( 158 array( 159 'taxonomy' => $taxonomy, 160 'name__like' => $search, 161 'fields' => 'names', 162 'hide_empty' => false, 163 'number' => isset( $_GET['number'] ) ? (int) $_GET['number'] : 0, 164 ) 165 ); 166 167 /** 168 * Filters the Ajax term search results. 169 * 170 * @since 6.1.0 171 * 172 * @param string[] $results Array of term names. 173 * @param WP_Taxonomy $taxonomy_object The taxonomy object. 174 * @param string $search The search term. 175 */ 176 $results = apply_filters( 'ajax_term_search_results', $results, $taxonomy_object, $search ); 177 178 echo implode( "\n", $results ); 179 wp_die(); 180 } 181 182 /** 183 * Handles image editor previews via AJAX. 184 * 185 * @since 3.1.0 186 */ 187 function wp_ajax_imgedit_preview() { 188 $post_id = (int) $_GET['postid']; 189 if ( empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { 190 wp_die( -1 ); 191 } 192 193 check_ajax_referer( "image_editor-$post_id" ); 194 195 require_once ABSPATH . 'wp-admin/includes/image-edit.php'; 196 197 if ( ! stream_preview_image( $post_id ) ) { 198 wp_die( -1 ); 199 } 200 201 wp_die(); 202 } 203 204 /** 205 * Handles oEmbed caching via AJAX. 206 * 207 * @since 3.1.0 208 * 209 * @global WP_Embed $wp_embed WordPress Embed object. 210 */ 211 function wp_ajax_oembed_cache() { 212 $GLOBALS['wp_embed']->cache_oembed( $_GET['post'] ); 213 wp_die( 0 ); 214 } 215 216 /** 217 * Handles user autocomplete via AJAX. 218 * 219 * @since 3.4.0 220 * @since 7.1.0 The search term is now sanitized, and a missing, non-string, 221 * or empty term results in a `0` response instead of an empty array. 222 * 223 * @return never 224 */ 225 function wp_ajax_autocomplete_user() { 226 if ( ! is_multisite() || ! current_user_can( 'promote_users' ) || wp_is_large_network( 'users' ) ) { 227 wp_die( -1 ); 228 } 229 230 /** This filter is documented in wp-admin/user-new.php */ 231 if ( ! current_user_can( 'manage_network_users' ) && ! apply_filters( 'autocomplete_users_for_site_admins', false ) ) { 232 wp_die( -1 ); 233 } 234 235 $return = array(); 236 237 // Obtain the search term, and short-circuit missing/invalid search term. 238 if ( ! isset( $_REQUEST['term'] ) || ! is_string( $_REQUEST['term'] ) ) { 239 wp_die( 0 ); 240 } 241 /* 242 * Asterisks are trimmed since wildcards are appended below. Without this, a 243 * term consisting only of asterisks would result in an empty search that 244 * matches all users. 245 */ 246 $term = trim( sanitize_text_field( wp_unslash( $_REQUEST['term'] ) ), '*' ); 247 if ( '' === $term ) { 248 wp_die( 0 ); 249 } 250 251 /* 252 * Check the type of request. 253 * Current allowed values are `add` and `search`. 254 */ 255 if ( isset( $_REQUEST['autocomplete_type'] ) && 'search' === $_REQUEST['autocomplete_type'] ) { 256 $type = $_REQUEST['autocomplete_type']; 257 } else { 258 $type = 'add'; 259 } 260 261 /* 262 * Check the desired field for value. 263 * Current allowed values are `user_email` and `user_login`. 264 */ 265 if ( isset( $_REQUEST['autocomplete_field'] ) && 'user_email' === $_REQUEST['autocomplete_field'] ) { 266 $field = $_REQUEST['autocomplete_field']; 267 } else { 268 $field = 'user_login'; 269 } 270 271 // Exclude current users of this blog. 272 if ( isset( $_REQUEST['site_id'] ) ) { 273 $id = absint( $_REQUEST['site_id'] ); 274 } else { 275 $id = get_current_blog_id(); 276 } 277 278 $include_blog_users = ( 'search' === $type ? get_users( 279 array( 280 'blog_id' => $id, 281 'fields' => 'ID', 282 ) 283 ) : array() ); 284 285 $exclude_blog_users = ( 'add' === $type ? get_users( 286 array( 287 'blog_id' => $id, 288 'fields' => 'ID', 289 ) 290 ) : array() ); 291 292 $users = get_users( 293 array( 294 'blog_id' => false, 295 'search' => '*' . $term . '*', 296 'include' => $include_blog_users, 297 'exclude' => $exclude_blog_users, 298 'search_columns' => array( 'user_login', 'user_nicename', 'user_email' ), 299 ) 300 ); 301 302 foreach ( $users as $user ) { 303 $return[] = array( 304 /* translators: 1: User login, 2: User email address. */ 305 'label' => sprintf( _x( '%1$s (%2$s)', 'user autocomplete result' ), $user->user_login, $user->user_email ), 306 'value' => $user->$field, 307 ); 308 } 309 310 wp_die( wp_json_encode( $return ) ); 311 } 312 313 /** 314 * Handles Ajax requests for community events 315 * 316 * @since 4.8.0 317 */ 318 function wp_ajax_get_community_events() { 319 require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php'; 320 321 check_ajax_referer( 'community_events' ); 322 323 $search = isset( $_POST['location'] ) ? wp_unslash( $_POST['location'] ) : ''; 324 $timezone = isset( $_POST['timezone'] ) ? wp_unslash( $_POST['timezone'] ) : ''; 325 $user_id = get_current_user_id(); 326 $saved_location = get_user_option( 'community-events-location', $user_id ); 327 $events_client = new WP_Community_Events( $user_id, $saved_location ); 328 $events = $events_client->get_events( $search, $timezone ); 329 $ip_changed = false; 330 331 if ( is_wp_error( $events ) ) { 332 wp_send_json_error( 333 array( 334 'error' => $events->get_error_message(), 335 ) 336 ); 337 } else { 338 if ( empty( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) ) { 339 $ip_changed = true; 340 } elseif ( isset( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) && $saved_location['ip'] !== $events['location']['ip'] ) { 341 $ip_changed = true; 342 } 343 344 /* 345 * The location should only be updated when it changes. The API doesn't always return 346 * a full location; sometimes it's missing the description or country. The location 347 * that was saved during the initial request is known to be good and complete, though. 348 * It should be left intact until the user explicitly changes it (either by manually 349 * searching for a new location, or by changing their IP address). 350 * 351 * If the location was updated with an incomplete response from the API, then it could 352 * break assumptions that the UI makes (e.g., that there will always be a description 353 * that corresponds to a latitude/longitude location). 354 * 355 * The location is stored network-wide, so that the user doesn't have to set it on each site. 356 */ 357 if ( $ip_changed || $search ) { 358 update_user_meta( $user_id, 'community-events-location', $events['location'] ); 359 } 360 361 wp_send_json_success( $events ); 362 } 363 } 364 365 /** 366 * Handles dashboard widgets via AJAX. 367 * 368 * @since 3.4.0 369 */ 370 function wp_ajax_dashboard_widgets() { 371 require_once ABSPATH . 'wp-admin/includes/dashboard.php'; 372 373 $pagenow = $_GET['pagenow']; 374 if ( 'dashboard-user' === $pagenow || 'dashboard-network' === $pagenow || 'dashboard' === $pagenow ) { 375 set_current_screen( $pagenow ); 376 } 377 378 switch ( $_GET['widget'] ) { 379 case 'dashboard_primary': 380 wp_dashboard_primary(); 381 break; 382 } 383 wp_die(); 384 } 385 386 /** 387 * Handles Customizer preview logged-in status via AJAX. 388 * 389 * @since 3.4.0 390 */ 391 function wp_ajax_logged_in() { 392 wp_die( 1 ); 393 } 394 395 // 396 // Ajax helpers. 397 // 398 399 /** 400 * Sends back current comment total and new page links if they need to be updated. 401 * 402 * Contrary to normal success Ajax response ("1"), die with time() on success. 403 * 404 * @since 2.7.0 405 * @access private 406 * 407 * @param int $comment_id Comment ID. 408 * @param int $delta Optional. Change in the number of total comments. Default -1. 409 */ 410 function _wp_ajax_delete_comment_response( $comment_id, $delta = -1 ) { 411 $total = isset( $_POST['_total'] ) ? (int) $_POST['_total'] : 0; 412 $per_page = isset( $_POST['_per_page'] ) ? (int) $_POST['_per_page'] : 0; 413 $page = isset( $_POST['_page'] ) ? (int) $_POST['_page'] : 0; 414 $url = isset( $_POST['_url'] ) ? sanitize_url( $_POST['_url'] ) : ''; 415 416 // JS didn't send us everything we need to know. Just die with success message. 417 if ( ! $total || ! $per_page || ! $page || ! $url ) { 418 $time = time(); 419 $comment = get_comment( $comment_id ); 420 $comment_status = ''; 421 $comment_link = ''; 422 423 if ( $comment ) { 424 $comment_status = $comment->comment_approved; 425 } 426 427 if ( 1 === (int) $comment_status ) { 428 $comment_link = get_comment_link( $comment ); 429 } 430 431 $counts = wp_count_comments(); 432 433 $response = new WP_Ajax_Response( 434 array( 435 'what' => 'comment', 436 // Here for completeness - not used. 437 'id' => $comment_id, 438 'supplemental' => array( 439 'status' => $comment_status, 440 'postId' => $comment ? $comment->comment_post_ID : '', 441 'time' => $time, 442 'in_moderation' => $counts->moderated, 443 'i18n_comments_text' => sprintf( 444 /* translators: %s: Number of comments. */ 445 _n( '%s Comment', '%s Comments', $counts->approved ), 446 number_format_i18n( $counts->approved ) 447 ), 448 'i18n_moderation_text' => sprintf( 449 /* translators: %s: Number of comments. */ 450 _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ), 451 number_format_i18n( $counts->moderated ) 452 ), 453 'comment_link' => $comment_link, 454 ), 455 ) 456 ); 457 $response->send(); 458 } 459 460 $total += $delta; 461 if ( $total < 0 ) { 462 $total = 0; 463 } 464 465 // Only do the expensive stuff on a page-break, and about 1 other time per page. 466 if ( 0 === $total % $per_page || 1 === mt_rand( 1, $per_page ) ) { 467 $post_id = 0; 468 // What type of comment count are we looking for? 469 $status = 'all'; 470 $parsed = parse_url( $url ); 471 472 if ( isset( $parsed['query'] ) ) { 473 parse_str( $parsed['query'], $query_vars ); 474 475 if ( ! empty( $query_vars['comment_status'] ) ) { 476 $status = $query_vars['comment_status']; 477 } 478 479 if ( ! empty( $query_vars['p'] ) ) { 480 $post_id = (int) $query_vars['p']; 481 } 482 483 if ( ! empty( $query_vars['comment_type'] ) ) { 484 $type = $query_vars['comment_type']; 485 } 486 } 487 488 if ( empty( $type ) ) { 489 // Only use the comment count if not filtering by a comment_type. 490 $comment_count = wp_count_comments( $post_id ); 491 492 // We're looking for a known type of comment count. 493 if ( isset( $comment_count->$status ) ) { 494 $total = $comment_count->$status; 495 } 496 } 497 // Else use the decremented value from above. 498 } 499 500 // The time since the last comment count. 501 $time = time(); 502 $comment = get_comment( $comment_id ); 503 $counts = wp_count_comments(); 504 505 $response = new WP_Ajax_Response( 506 array( 507 'what' => 'comment', 508 'id' => $comment_id, 509 'supplemental' => array( 510 'status' => $comment ? $comment->comment_approved : '', 511 'postId' => $comment ? $comment->comment_post_ID : '', 512 /* translators: %s: Number of comments. */ 513 'total_items_i18n' => sprintf( _n( '%s item', '%s items', $total ), number_format_i18n( $total ) ), 514 'total_pages' => (int) ceil( $total / $per_page ), 515 'total_pages_i18n' => number_format_i18n( (int) ceil( $total / $per_page ) ), 516 'total' => $total, 517 'time' => $time, 518 'in_moderation' => $counts->moderated, 519 'i18n_moderation_text' => sprintf( 520 /* translators: %s: Number of comments. */ 521 _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ), 522 number_format_i18n( $counts->moderated ) 523 ), 524 ), 525 ) 526 ); 527 $response->send(); 528 } 529 530 // 531 // POST-based Ajax handlers. 532 // 533 534 /** 535 * Handles adding a hierarchical term via AJAX. 536 * 537 * @since 3.1.0 538 * @access private 539 */ 540 function _wp_ajax_add_hierarchical_term() { 541 $action = $_POST['action']; 542 $taxonomy = get_taxonomy( substr( $action, 4 ) ); 543 check_ajax_referer( $action, '_ajax_nonce-add-' . $taxonomy->name ); 544 545 if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) { 546 wp_die( -1 ); 547 } 548 549 $names = explode( ',', $_POST[ 'new' . $taxonomy->name ] ); 550 $parent = isset( $_POST[ 'new' . $taxonomy->name . '_parent' ] ) ? (int) $_POST[ 'new' . $taxonomy->name . '_parent' ] : 0; 551 552 if ( 0 > $parent ) { 553 $parent = 0; 554 } 555 556 if ( 'category' === $taxonomy->name ) { 557 $post_category = isset( $_POST['post_category'] ) ? (array) $_POST['post_category'] : array(); 558 } else { 559 $post_category = ( isset( $_POST['tax_input'] ) && isset( $_POST['tax_input'][ $taxonomy->name ] ) ) ? (array) $_POST['tax_input'][ $taxonomy->name ] : array(); 560 } 561 562 $checked_categories = array_map( 'absint', (array) $post_category ); 563 $popular_ids = wp_popular_terms_checklist( $taxonomy->name, 0, 10, false ); 564 565 foreach ( $names as $category_name ) { 566 $category_name = trim( $category_name ); 567 $category_nicename = sanitize_title( $category_name ); 568 569 if ( '' === $category_nicename ) { 570 continue; 571 } 572 573 $category_id = wp_insert_term( $category_name, $taxonomy->name, array( 'parent' => $parent ) ); 574 575 if ( ! $category_id || is_wp_error( $category_id ) ) { 576 continue; 577 } else { 578 $category_id = $category_id['term_id']; 579 } 580 581 $checked_categories[] = $category_id; 582 583 if ( $parent ) { // Do these all at once in a second. 584 continue; 585 } 586 587 ob_start(); 588 589 wp_terms_checklist( 590 0, 591 array( 592 'taxonomy' => $taxonomy->name, 593 'descendants_and_self' => $category_id, 594 'selected_cats' => $checked_categories, 595 'popular_cats' => $popular_ids, 596 ) 597 ); 598 599 $data = ob_get_clean(); 600 601 $add = array( 602 'what' => $taxonomy->name, 603 'id' => $category_id, 604 'data' => str_replace( array( "\n", "\t" ), '', $data ), 605 'position' => -1, 606 ); 607 } 608 609 if ( $parent ) { // Foncy - replace the parent and all its children. 610 $parent = get_term( $parent, $taxonomy->name ); 611 $term_id = $parent->term_id; 612 613 while ( $parent->parent ) { // Get the top parent. 614 $parent = get_term( $parent->parent, $taxonomy->name ); 615 if ( is_wp_error( $parent ) ) { 616 break; 617 } 618 $term_id = $parent->term_id; 619 } 620 621 ob_start(); 622 623 wp_terms_checklist( 624 0, 625 array( 626 'taxonomy' => $taxonomy->name, 627 'descendants_and_self' => $term_id, 628 'selected_cats' => $checked_categories, 629 'popular_cats' => $popular_ids, 630 ) 631 ); 632 633 $data = ob_get_clean(); 634 635 $add = array( 636 'what' => $taxonomy->name, 637 'id' => $term_id, 638 'data' => str_replace( array( "\n", "\t" ), '', $data ), 639 'position' => -1, 640 ); 641 } 642 643 $parent_dropdown_args = array( 644 'taxonomy' => $taxonomy->name, 645 'hide_empty' => 0, 646 'name' => 'new' . $taxonomy->name . '_parent', 647 'orderby' => 'name', 648 'hierarchical' => 1, 649 'show_option_none' => '— ' . $taxonomy->labels->parent_item . ' —', 650 ); 651 652 /** This filter is documented in wp-admin/includes/meta-boxes.php */ 653 $parent_dropdown_args = apply_filters( 'post_edit_category_parent_dropdown_args', $parent_dropdown_args ); 654 655 ob_start(); 656 657 wp_dropdown_categories( $parent_dropdown_args ); 658 659 $supplemental = ob_get_clean(); 660 661 $add['supplemental'] = array( 'newcat_parent' => $supplemental ); 662 663 $response = new WP_Ajax_Response( $add ); 664 $response->send(); 665 } 666 667 /** 668 * Handles deleting a comment via AJAX. 669 * 670 * @since 3.1.0 671 */ 672 function wp_ajax_delete_comment() { 673 $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 674 675 $comment = get_comment( $id ); 676 677 if ( ! $comment ) { 678 wp_die( time() ); 679 } 680 681 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) { 682 wp_die( -1 ); 683 } 684 685 check_ajax_referer( "delete-comment_$id" ); 686 $status = wp_get_comment_status( $comment ); 687 $delta = -1; 688 689 if ( isset( $_POST['trash'] ) && '1' === $_POST['trash'] ) { 690 if ( 'trash' === $status ) { 691 wp_die( time() ); 692 } 693 694 $result = wp_trash_comment( $comment ); 695 } elseif ( isset( $_POST['untrash'] ) && '1' === $_POST['untrash'] ) { 696 if ( 'trash' !== $status ) { 697 wp_die( time() ); 698 } 699 700 $result = wp_untrash_comment( $comment ); 701 702 // Undo trash, not in Trash. 703 if ( ! isset( $_POST['comment_status'] ) || 'trash' !== $_POST['comment_status'] ) { 704 $delta = 1; 705 } 706 } elseif ( isset( $_POST['spam'] ) && '1' === $_POST['spam'] ) { 707 if ( 'spam' === $status ) { 708 wp_die( time() ); 709 } 710 711 $result = wp_spam_comment( $comment ); 712 } elseif ( isset( $_POST['unspam'] ) && '1' === $_POST['unspam'] ) { 713 if ( 'spam' !== $status ) { 714 wp_die( time() ); 715 } 716 717 $result = wp_unspam_comment( $comment ); 718 719 // Undo spam, not in spam. 720 if ( ! isset( $_POST['comment_status'] ) || 'spam' !== $_POST['comment_status'] ) { 721 $delta = 1; 722 } 723 } elseif ( isset( $_POST['delete'] ) && '1' === $_POST['delete'] ) { 724 $result = wp_delete_comment( $comment ); 725 } else { 726 wp_die( -1 ); 727 } 728 729 if ( $result ) { 730 // Decide if we need to send back '1' or a more complicated response including page links and comment counts. 731 _wp_ajax_delete_comment_response( $comment->comment_ID, $delta ); 732 } 733 734 wp_die( 0 ); 735 } 736 737 /** 738 * Handles deleting a tag via AJAX. 739 * 740 * @since 3.1.0 741 */ 742 function wp_ajax_delete_tag() { 743 $tag_id = (int) $_POST['tag_ID']; 744 check_ajax_referer( "delete-tag_$tag_id" ); 745 746 if ( ! current_user_can( 'delete_term', $tag_id ) ) { 747 wp_die( -1 ); 748 } 749 750 $taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag'; 751 $tag = get_term( $tag_id, $taxonomy ); 752 753 if ( ! $tag || is_wp_error( $tag ) ) { 754 wp_die( 1 ); 755 } 756 757 if ( wp_delete_term( $tag_id, $taxonomy ) ) { 758 wp_die( 1 ); 759 } else { 760 wp_die( 0 ); 761 } 762 } 763 764 /** 765 * Handles deleting a link via AJAX. 766 * 767 * @since 3.1.0 768 */ 769 function wp_ajax_delete_link() { 770 $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 771 772 check_ajax_referer( "delete-bookmark_$id" ); 773 774 if ( ! current_user_can( 'manage_links' ) ) { 775 wp_die( -1 ); 776 } 777 778 $link = get_bookmark( $id ); 779 if ( ! $link || is_wp_error( $link ) ) { 780 wp_die( 1 ); 781 } 782 783 if ( wp_delete_link( $id ) ) { 784 wp_die( 1 ); 785 } else { 786 wp_die( 0 ); 787 } 788 } 789 790 /** 791 * Handles deleting meta via AJAX. 792 * 793 * @since 3.1.0 794 */ 795 function wp_ajax_delete_meta() { 796 $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 797 798 check_ajax_referer( "delete-meta_$id" ); 799 $meta = get_metadata_by_mid( 'post', $id ); 800 801 if ( ! $meta ) { 802 wp_die( 1 ); 803 } 804 805 if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $meta->post_id, $meta->meta_key ) ) { 806 wp_die( -1 ); 807 } 808 809 if ( delete_meta( $meta->meta_id ) ) { 810 wp_die( 1 ); 811 } 812 813 wp_die( 0 ); 814 } 815 816 /** 817 * Handles deleting a post via AJAX. 818 * 819 * @since 3.1.0 820 * 821 * @param string $action Action to perform. 822 */ 823 function wp_ajax_delete_post( $action ) { 824 if ( empty( $action ) ) { 825 $action = 'delete-post'; 826 } 827 828 $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 829 check_ajax_referer( "{$action}_$id" ); 830 831 if ( ! current_user_can( 'delete_post', $id ) ) { 832 wp_die( -1 ); 833 } 834 835 if ( ! get_post( $id ) ) { 836 wp_die( 1 ); 837 } 838 839 if ( wp_delete_post( $id ) ) { 840 wp_die( 1 ); 841 } else { 842 wp_die( 0 ); 843 } 844 } 845 846 /** 847 * Handles sending a post to the Trash via AJAX. 848 * 849 * @since 3.1.0 850 * 851 * @param string $action Action to perform. 852 */ 853 function wp_ajax_trash_post( $action ) { 854 if ( empty( $action ) ) { 855 $action = 'trash-post'; 856 } 857 858 $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 859 check_ajax_referer( "{$action}_$id" ); 860 861 if ( ! current_user_can( 'delete_post', $id ) ) { 862 wp_die( -1 ); 863 } 864 865 if ( ! get_post( $id ) ) { 866 wp_die( 1 ); 867 } 868 869 if ( 'trash-post' === $action ) { 870 $done = wp_trash_post( $id ); 871 } else { 872 $done = wp_untrash_post( $id ); 873 } 874 875 if ( $done ) { 876 wp_die( 1 ); 877 } 878 879 wp_die( 0 ); 880 } 881 882 /** 883 * Handles restoring a post from the Trash via AJAX. 884 * 885 * @since 3.1.0 886 * 887 * @param string $action Action to perform. 888 */ 889 function wp_ajax_untrash_post( $action ) { 890 if ( empty( $action ) ) { 891 $action = 'untrash-post'; 892 } 893 894 wp_ajax_trash_post( $action ); 895 } 896 897 /** 898 * Handles deleting a page via AJAX. 899 * 900 * @since 3.1.0 901 * 902 * @param string $action Action to perform. 903 */ 904 function wp_ajax_delete_page( $action ) { 905 if ( empty( $action ) ) { 906 $action = 'delete-page'; 907 } 908 909 $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 910 check_ajax_referer( "{$action}_$id" ); 911 912 if ( ! current_user_can( 'delete_page', $id ) ) { 913 wp_die( -1 ); 914 } 915 916 if ( ! get_post( $id ) ) { 917 wp_die( 1 ); 918 } 919 920 if ( wp_delete_post( $id ) ) { 921 wp_die( 1 ); 922 } else { 923 wp_die( 0 ); 924 } 925 } 926 927 /** 928 * Handles dimming a comment via AJAX. 929 * 930 * @since 3.1.0 931 */ 932 function wp_ajax_dim_comment() { 933 $id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0; 934 $comment = get_comment( $id ); 935 936 if ( ! $comment ) { 937 $response = new WP_Ajax_Response( 938 array( 939 'what' => 'comment', 940 'id' => new WP_Error( 941 'invalid_comment', 942 /* translators: %d: Comment ID. */ 943 sprintf( __( 'Comment %d does not exist' ), $id ) 944 ), 945 ) 946 ); 947 $response->send(); 948 } 949 950 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && ! current_user_can( 'moderate_comments' ) ) { 951 wp_die( -1 ); 952 } 953 954 $current = wp_get_comment_status( $comment ); 955 956 if ( isset( $_POST['new'] ) && $_POST['new'] === $current ) { 957 wp_die( time() ); 958 } 959 960 check_ajax_referer( "approve-comment_$id" ); 961 962 if ( in_array( $current, array( 'unapproved', 'spam' ), true ) ) { 963 $result = wp_set_comment_status( $comment, 'approve', true ); 964 } else { 965 $result = wp_set_comment_status( $comment, 'hold', true ); 966 } 967 968 if ( is_wp_error( $result ) ) { 969 $response = new WP_Ajax_Response( 970 array( 971 'what' => 'comment', 972 'id' => $result, 973 ) 974 ); 975 $response->send(); 976 } 977 978 // Decide if we need to send back '1' or a more complicated response including page links and comment counts. 979 _wp_ajax_delete_comment_response( $comment->comment_ID ); 980 wp_die( 0 ); 981 } 982 983 /** 984 * Handles adding a link category via AJAX. 985 * 986 * @since 3.1.0 987 * 988 * @param string $action Action to perform. 989 */ 990 function wp_ajax_add_link_category( $action ) { 991 if ( empty( $action ) ) { 992 $action = 'add-link-category'; 993 } 994 995 check_ajax_referer( $action ); 996 997 $taxonomy_object = get_taxonomy( 'link_category' ); 998 999 if ( ! current_user_can( $taxonomy_object->cap->manage_terms ) ) { 1000 wp_die( -1 ); 1001 } 1002 1003 $names = explode( ',', wp_unslash( $_POST['newcat'] ) ); 1004 $response = new WP_Ajax_Response(); 1005 1006 foreach ( $names as $category_name ) { 1007 $category_name = trim( $category_name ); 1008 $slug = sanitize_title( $category_name ); 1009 1010 if ( '' === $slug ) { 1011 continue; 1012 } 1013 1014 $category_id = wp_insert_term( $category_name, 'link_category' ); 1015 1016 if ( ! $category_id || is_wp_error( $category_id ) ) { 1017 continue; 1018 } else { 1019 $category_id = $category_id['term_id']; 1020 } 1021 1022 $category_name = esc_html( $category_name ); 1023 1024 $response->add( 1025 array( 1026 'what' => 'link-category', 1027 'id' => $category_id, 1028 'data' => "<li id='link-category-$category_id'><label for='in-link-category-$category_id' class='selectit'><input value='" . esc_attr( $category_id ) . "' type='checkbox' checked='checked' name='link_category[]' id='in-link-category-$category_id'/> $category_name</label></li>", 1029 'position' => -1, 1030 ) 1031 ); 1032 } 1033 1034 $response->send(); 1035 } 1036 1037 /** 1038 * Handles adding a tag via AJAX. 1039 * 1040 * @since 3.1.0 1041 */ 1042 function wp_ajax_add_tag() { 1043 check_ajax_referer( 'add-tag', '_wpnonce_add-tag' ); 1044 1045 $taxonomy = ! empty( $_POST['taxonomy'] ) ? $_POST['taxonomy'] : 'post_tag'; 1046 $taxonomy_object = get_taxonomy( $taxonomy ); 1047 1048 if ( ! current_user_can( $taxonomy_object->cap->edit_terms ) ) { 1049 wp_die( -1 ); 1050 } 1051 1052 $response = new WP_Ajax_Response(); 1053 1054 $tag = wp_insert_term( $_POST['tag-name'], $taxonomy, $_POST ); 1055 1056 if ( $tag && ! is_wp_error( $tag ) ) { 1057 $tag = get_term( $tag['term_id'], $taxonomy ); 1058 } 1059 1060 if ( ! $tag || is_wp_error( $tag ) ) { 1061 $message = __( 'An error has occurred. Please reload the page and try again.' ); 1062 $error_code = 'error'; 1063 1064 if ( is_wp_error( $tag ) && $tag->get_error_message() ) { 1065 $message = $tag->get_error_message(); 1066 } 1067 1068 if ( is_wp_error( $tag ) && $tag->get_error_code() ) { 1069 $error_code = $tag->get_error_code(); 1070 } 1071 1072 $response->add( 1073 array( 1074 'what' => 'taxonomy', 1075 'data' => new WP_Error( $error_code, $message ), 1076 ) 1077 ); 1078 $response->send(); 1079 } 1080 1081 $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => $_POST['screen'] ) ); 1082 1083 $level = 0; 1084 $no_parents = ''; 1085 1086 if ( is_taxonomy_hierarchical( $taxonomy ) ) { 1087 $level = count( get_ancestors( $tag->term_id, $taxonomy, 'taxonomy' ) ); 1088 ob_start(); 1089 $wp_list_table->single_row( $tag, $level ); 1090 $no_parents = ob_get_clean(); 1091 } 1092 1093 ob_start(); 1094 $wp_list_table->single_row( $tag ); 1095 $parents = ob_get_clean(); 1096 1097 require ABSPATH . 'wp-admin/includes/edit-tag-messages.php'; 1098 1099 $message = ''; 1100 if ( isset( $messages[ $taxonomy_object->name ][1] ) ) { 1101 $message = $messages[ $taxonomy_object->name ][1]; 1102 } elseif ( isset( $messages['_item'][1] ) ) { 1103 $message = $messages['_item'][1]; 1104 } 1105 1106 $response->add( 1107 array( 1108 'what' => 'taxonomy', 1109 'data' => $message, 1110 'supplemental' => array( 1111 'parents' => $parents, 1112 'noparents' => $no_parents, 1113 'notice' => $message, 1114 ), 1115 ) 1116 ); 1117 1118 $response->add( 1119 array( 1120 'what' => 'term', 1121 'position' => $level, 1122 'supplemental' => (array) $tag, 1123 ) 1124 ); 1125 1126 $response->send(); 1127 } 1128 1129 /** 1130 * Handles getting a tagcloud via AJAX. 1131 * 1132 * @since 3.1.0 1133 */ 1134 function wp_ajax_get_tagcloud() { 1135 if ( ! isset( $_POST['tax'] ) ) { 1136 wp_die( 0 ); 1137 } 1138 1139 $taxonomy = sanitize_key( $_POST['tax'] ); 1140 $taxonomy_object = get_taxonomy( $taxonomy ); 1141 1142 if ( ! $taxonomy_object ) { 1143 wp_die( 0 ); 1144 } 1145 1146 if ( ! current_user_can( $taxonomy_object->cap->assign_terms ) ) { 1147 wp_die( -1 ); 1148 } 1149 1150 $tags = get_terms( 1151 array( 1152 'taxonomy' => $taxonomy, 1153 'number' => 45, 1154 'orderby' => 'count', 1155 'order' => 'DESC', 1156 ) 1157 ); 1158 1159 if ( empty( $tags ) ) { 1160 wp_die( $taxonomy_object->labels->not_found ); 1161 } 1162 1163 if ( is_wp_error( $tags ) ) { 1164 wp_die( $tags->get_error_message() ); 1165 } 1166 1167 foreach ( $tags as $key => $tag ) { 1168 $tags[ $key ]->link = '#'; 1169 $tags[ $key ]->id = $tag->term_id; 1170 } 1171 1172 // We need raw tag names here, so don't filter the output. 1173 $return = wp_generate_tag_cloud( 1174 $tags, 1175 array( 1176 'filter' => 0, 1177 'format' => 'list', 1178 ) 1179 ); 1180 1181 if ( empty( $return ) ) { 1182 wp_die( 0 ); 1183 } 1184 1185 echo $return; 1186 wp_die(); 1187 } 1188 1189 /** 1190 * Handles getting comments via AJAX. 1191 * 1192 * @since 3.1.0 1193 * 1194 * @global int $post_id Post ID. 1195 * 1196 * @param string $action Action to perform. 1197 */ 1198 function wp_ajax_get_comments( $action ) { 1199 global $post_id; 1200 1201 if ( empty( $action ) ) { 1202 $action = 'get-comments'; 1203 } 1204 1205 check_ajax_referer( $action ); 1206 1207 if ( empty( $post_id ) && ! empty( $_REQUEST['p'] ) ) { 1208 $id = absint( $_REQUEST['p'] ); 1209 if ( ! empty( $id ) ) { 1210 $post_id = $id; 1211 } 1212 } 1213 1214 if ( empty( $post_id ) ) { 1215 wp_die( -1 ); 1216 } 1217 1218 $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 1219 1220 if ( ! current_user_can( 'edit_post', $post_id ) ) { 1221 wp_die( -1 ); 1222 } 1223 1224 $wp_list_table->prepare_items(); 1225 1226 if ( ! $wp_list_table->has_items() ) { 1227 wp_die( 1 ); 1228 } 1229 1230 $response = new WP_Ajax_Response(); 1231 1232 ob_start(); 1233 foreach ( $wp_list_table->items as $comment ) { 1234 if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) && 0 === $comment->comment_approved ) { 1235 continue; 1236 } 1237 get_comment( $comment ); 1238 $wp_list_table->single_row( $comment ); 1239 } 1240 $comment_list_item = ob_get_clean(); 1241 1242 $response->add( 1243 array( 1244 'what' => 'comments', 1245 'data' => $comment_list_item, 1246 ) 1247 ); 1248 1249 $response->send(); 1250 } 1251 1252 /** 1253 * Handles replying to a comment via AJAX. 1254 * 1255 * @since 3.1.0 1256 * 1257 * @param string $action Action to perform. 1258 */ 1259 function wp_ajax_replyto_comment( $action ) { 1260 if ( empty( $action ) ) { 1261 $action = 'replyto-comment'; 1262 } 1263 1264 check_ajax_referer( $action, '_ajax_nonce-replyto-comment' ); 1265 1266 $comment_post_id = (int) $_POST['comment_post_ID']; 1267 $post = get_post( $comment_post_id ); 1268 1269 if ( ! $post ) { 1270 wp_die( -1 ); 1271 } 1272 1273 if ( ! current_user_can( 'edit_post', $comment_post_id ) ) { 1274 wp_die( -1 ); 1275 } 1276 1277 if ( empty( $post->post_status ) ) { 1278 wp_die( 1 ); 1279 } elseif ( in_array( $post->post_status, array( 'draft', 'pending', 'trash' ), true ) ) { 1280 wp_die( __( 'You cannot reply to a comment on a draft post.' ) ); 1281 } 1282 1283 $user = wp_get_current_user(); 1284 1285 if ( $user->exists() ) { 1286 $comment_author = wp_slash( $user->display_name ); 1287 $comment_author_email = wp_slash( $user->user_email ); 1288 $comment_author_url = wp_slash( $user->user_url ); 1289 $user_id = $user->ID; 1290 1291 if ( current_user_can( 'unfiltered_html' ) ) { 1292 if ( ! isset( $_POST['_wp_unfiltered_html_comment'] ) ) { 1293 $_POST['_wp_unfiltered_html_comment'] = ''; 1294 } 1295 1296 if ( wp_create_nonce( 'unfiltered-html-comment' ) !== $_POST['_wp_unfiltered_html_comment'] ) { 1297 kses_remove_filters(); // Start with a clean slate. 1298 kses_init_filters(); // Set up the filters. 1299 remove_filter( 'pre_comment_content', 'wp_filter_post_kses' ); 1300 add_filter( 'pre_comment_content', 'wp_filter_kses' ); 1301 } 1302 } 1303 } else { 1304 wp_die( __( 'Sorry, you must be logged in to reply to a comment.' ) ); 1305 } 1306 1307 $comment_content = trim( $_POST['content'] ); 1308 1309 if ( '' === $comment_content ) { 1310 wp_die( __( 'Please type your comment text.' ) ); 1311 } 1312 1313 $comment_type = isset( $_POST['comment_type'] ) ? trim( $_POST['comment_type'] ) : 'comment'; 1314 1315 /* 1316 * Notes and reactions are only created through the REST API. The lowercase 1317 * ASCII limit also catches spellings the case- and accent-insensitive 1318 * collation would match. 1319 */ 1320 if ( 1321 ! preg_match( '/^[a-z0-9_-]+\z/', $comment_type ) || 1322 in_array( $comment_type, array( 'note', 'reaction' ), true ) 1323 ) { 1324 wp_die( -1 ); 1325 } 1326 1327 $comment_parent = 0; 1328 1329 if ( isset( $_POST['comment_ID'] ) ) { 1330 $comment_parent = absint( $_POST['comment_ID'] ); 1331 } 1332 1333 // A comment on another post cannot be the parent. 1334 if ( $comment_parent ) { 1335 $parent = get_comment( $comment_parent ); 1336 1337 if ( $parent && (int) $parent->comment_post_ID !== $comment_post_id ) { 1338 $comment_parent = 0; 1339 } 1340 } 1341 1342 $comment_auto_approved = false; 1343 1344 $commentdata = array( 1345 'comment_post_ID' => $comment_post_id, 1346 ); 1347 1348 $commentdata += compact( 1349 'comment_author', 1350 'comment_author_email', 1351 'comment_author_url', 1352 'comment_content', 1353 'comment_type', 1354 'comment_parent', 1355 'user_id' 1356 ); 1357 1358 // Automatically approve parent comment. 1359 if ( ! empty( $_POST['approve_parent'] ) ) { 1360 $parent = get_comment( $comment_parent ); 1361 1362 if ( $parent && '0' === $parent->comment_approved && (int) $parent->comment_post_ID === $comment_post_id ) { 1363 if ( ! current_user_can( 'edit_comment', $parent->comment_ID ) ) { 1364 wp_die( -1 ); 1365 } 1366 1367 if ( wp_set_comment_status( $parent, 'approve' ) ) { 1368 $comment_auto_approved = true; 1369 } 1370 } 1371 } 1372 1373 $comment_id = wp_new_comment( $commentdata ); 1374 1375 if ( is_wp_error( $comment_id ) ) { 1376 wp_die( $comment_id->get_error_message() ); 1377 } 1378 1379 $comment = get_comment( $comment_id ); 1380 1381 if ( ! $comment ) { 1382 wp_die( 1 ); 1383 } 1384 1385 $position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1'; 1386 1387 ob_start(); 1388 if ( isset( $_REQUEST['mode'] ) && 'dashboard' === $_REQUEST['mode'] ) { 1389 require_once ABSPATH . 'wp-admin/includes/dashboard.php'; 1390 _wp_dashboard_recent_comments_row( $comment ); 1391 } else { 1392 if ( isset( $_REQUEST['mode'] ) && 'single' === $_REQUEST['mode'] ) { 1393 $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 1394 } else { 1395 $wp_list_table = _get_list_table( 'WP_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 1396 } 1397 $wp_list_table->single_row( $comment ); 1398 } 1399 $comment_list_item = ob_get_clean(); 1400 1401 $response_data = array( 1402 'what' => 'comment', 1403 'id' => $comment->comment_ID, 1404 'data' => $comment_list_item, 1405 'position' => $position, 1406 ); 1407 1408 $counts = wp_count_comments(); 1409 1410 $response_data['supplemental'] = array( 1411 'in_moderation' => $counts->moderated, 1412 'i18n_comments_text' => sprintf( 1413 /* translators: %s: Number of comments. */ 1414 _n( '%s Comment', '%s Comments', $counts->approved ), 1415 number_format_i18n( $counts->approved ) 1416 ), 1417 'i18n_moderation_text' => sprintf( 1418 /* translators: %s: Number of comments. */ 1419 _n( '%s Comment in moderation', '%s Comments in moderation', $counts->moderated ), 1420 number_format_i18n( $counts->moderated ) 1421 ), 1422 ); 1423 1424 if ( $comment_auto_approved ) { 1425 $response_data['supplemental']['parent_approved'] = $parent->comment_ID; 1426 $response_data['supplemental']['parent_post_id'] = $parent->comment_post_ID; 1427 } 1428 1429 $response = new WP_Ajax_Response(); 1430 $response->add( $response_data ); 1431 $response->send(); 1432 } 1433 1434 /** 1435 * Handles editing a comment via AJAX. 1436 * 1437 * @since 3.1.0 1438 */ 1439 function wp_ajax_edit_comment() { 1440 check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ); 1441 1442 $comment_id = (int) $_POST['comment_ID']; 1443 1444 if ( ! current_user_can( 'edit_comment', $comment_id ) ) { 1445 wp_die( -1 ); 1446 } 1447 1448 if ( '' === $_POST['content'] ) { 1449 wp_die( __( 'Please type your comment text.' ) ); 1450 } 1451 1452 if ( isset( $_POST['status'] ) ) { 1453 $_POST['comment_status'] = $_POST['status']; 1454 } 1455 1456 $updated = edit_comment(); 1457 if ( is_wp_error( $updated ) ) { 1458 wp_die( $updated->get_error_message() ); 1459 } 1460 1461 $position = ( isset( $_POST['position'] ) && (int) $_POST['position'] ) ? (int) $_POST['position'] : '-1'; 1462 /* 1463 * Checkbox is used to differentiate between the Edit Comments screen (1) 1464 * and the Comments section on the Edit Post screen (0). 1465 */ 1466 $checkbox = ( isset( $_POST['checkbox'] ) && '1' === $_POST['checkbox'] ) ? 1 : 0; 1467 $wp_list_table = _get_list_table( $checkbox ? 'WP_Comments_List_Table' : 'WP_Post_Comments_List_Table', array( 'screen' => 'edit-comments' ) ); 1468 1469 $comment = get_comment( $comment_id ); 1470 1471 if ( empty( $comment->comment_ID ) ) { 1472 wp_die( -1 ); 1473 } 1474 1475 ob_start(); 1476 $wp_list_table->single_row( $comment ); 1477 $comment_list_item = ob_get_clean(); 1478 1479 $response = new WP_Ajax_Response(); 1480 1481 $response->add( 1482 array( 1483 'what' => 'edit_comment', 1484 'id' => $comment->comment_ID, 1485 'data' => $comment_list_item, 1486 'position' => $position, 1487 ) 1488 ); 1489 1490 $response->send(); 1491 } 1492 1493 /** 1494 * Handles adding a menu item via AJAX. 1495 * 1496 * @since 3.1.0 1497 */ 1498 function wp_ajax_add_menu_item() { 1499 check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' ); 1500 1501 if ( ! current_user_can( 'edit_theme_options' ) ) { 1502 wp_die( -1 ); 1503 } 1504 1505 require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; 1506 1507 /* 1508 * For performance reasons, we omit some object properties from the checklist. 1509 * The following is a hacky way to restore them when adding non-custom items. 1510 */ 1511 $menu_items_data = array(); 1512 1513 foreach ( (array) $_POST['menu-item'] as $menu_item_data ) { 1514 if ( 1515 ! empty( $menu_item_data['menu-item-type'] ) && 1516 'custom' !== $menu_item_data['menu-item-type'] && 1517 ! empty( $menu_item_data['menu-item-object-id'] ) 1518 ) { 1519 switch ( $menu_item_data['menu-item-type'] ) { 1520 case 'post_type': 1521 $_object = get_post( $menu_item_data['menu-item-object-id'] ); 1522 break; 1523 1524 case 'post_type_archive': 1525 $_object = get_post_type_object( $menu_item_data['menu-item-object'] ); 1526 break; 1527 1528 case 'taxonomy': 1529 $_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] ); 1530 break; 1531 } 1532 1533 $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) ); 1534 $_menu_item = reset( $_menu_items ); 1535 1536 // Restore the missing menu item properties. 1537 $menu_item_data['menu-item-description'] = $_menu_item->description; 1538 } 1539 1540 $menu_items_data[] = $menu_item_data; 1541 } 1542 1543 $item_ids = wp_save_nav_menu_items( 0, $menu_items_data ); 1544 if ( is_wp_error( $item_ids ) ) { 1545 wp_die( 0 ); 1546 } 1547 1548 $menu_items = array(); 1549 1550 foreach ( (array) $item_ids as $menu_item_id ) { 1551 $menu_object = get_post( $menu_item_id ); 1552 1553 if ( ! empty( $menu_object->ID ) ) { 1554 $menu_object = wp_setup_nav_menu_item( $menu_object ); 1555 $menu_object->title = empty( $menu_object->title ) ? __( 'Menu Item' ) : $menu_object->title; 1556 $menu_object->label = $menu_object->title; // Don't show "(pending)" in ajax-added items. 1557 $menu_items[] = $menu_object; 1558 } 1559 } 1560 1561 /** This filter is documented in wp-admin/includes/nav-menu.php */ 1562 $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu'] ); 1563 1564 if ( ! class_exists( $walker_class_name ) ) { 1565 wp_die( 0 ); 1566 } 1567 1568 if ( ! empty( $menu_items ) ) { 1569 $args = array( 1570 'after' => '', 1571 'before' => '', 1572 'link_after' => '', 1573 'link_before' => '', 1574 'walker' => new $walker_class_name(), 1575 ); 1576 1577 echo walk_nav_menu_tree( $menu_items, 0, (object) $args ); 1578 } 1579 1580 wp_die(); 1581 } 1582 1583 /** 1584 * Handles adding meta via AJAX. 1585 * 1586 * @since 3.1.0 1587 */ 1588 function wp_ajax_add_meta() { 1589 check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' ); 1590 $count = 0; 1591 $post_id = (int) $_POST['post_id']; 1592 $post = get_post( $post_id ); 1593 1594 if ( isset( $_POST['metakeyselect'] ) || isset( $_POST['metakeyinput'] ) ) { 1595 if ( ! $post || ! current_user_can( 'edit_post', $post_id ) ) { 1596 wp_die( -1 ); 1597 } 1598 1599 if ( isset( $_POST['metakeyselect'] ) && '#NONE#' === $_POST['metakeyselect'] && empty( $_POST['metakeyinput'] ) ) { 1600 wp_die( 1 ); 1601 } 1602 1603 // If the post is an autodraft, save the post as a draft and then attempt to save the meta. 1604 if ( 'auto-draft' === $post->post_status ) { 1605 $post_data = array(); 1606 $post_data['action'] = 'draft'; // Warning fix. 1607 $post_data['post_ID'] = $post_id; 1608 $post_data['post_type'] = $post->post_type; 1609 $post_data['post_status'] = 'draft'; 1610 $now = time(); 1611 1612 $post_data['post_title'] = sprintf( 1613 /* translators: 1: Post creation date, 2: Post creation time. */ 1614 __( 'Draft created on %1$s at %2$s' ), 1615 gmdate( __( 'F j, Y' ), $now ), 1616 gmdate( __( 'g:i a' ), $now ) 1617 ); 1618 1619 $post_id = edit_post( $post_data ); 1620 1621 if ( $post_id ) { 1622 if ( is_wp_error( $post_id ) ) { 1623 $response = new WP_Ajax_Response( 1624 array( 1625 'what' => 'meta', 1626 'data' => $post_id, 1627 ) 1628 ); 1629 $response->send(); 1630 } 1631 1632 $meta_id = add_meta( $post_id ); 1633 1634 if ( ! $meta_id ) { 1635 wp_die( __( 'Please provide a custom field value.' ) ); 1636 } 1637 } else { 1638 wp_die( 0 ); 1639 } 1640 } else { 1641 $meta_id = add_meta( $post_id ); 1642 1643 if ( ! $meta_id ) { 1644 wp_die( __( 'Please provide a custom field value.' ) ); 1645 } 1646 } 1647 1648 $meta = get_metadata_by_mid( 'post', $meta_id ); 1649 $post_id = (int) $meta->post_id; 1650 $meta = get_object_vars( $meta ); 1651 1652 $response = new WP_Ajax_Response( 1653 array( 1654 'what' => 'meta', 1655 'id' => $meta_id, 1656 'data' => _list_meta_row( $meta, $count ), 1657 'position' => 1, 1658 'supplemental' => array( 'postid' => $post_id ), 1659 ) 1660 ); 1661 } else { // Update? 1662 $meta_id = (int) key( $_POST['meta'] ); 1663 $key = wp_unslash( $_POST['meta'][ $meta_id ]['key'] ); 1664 $value = wp_unslash( $_POST['meta'][ $meta_id ]['value'] ); 1665 1666 if ( '' === trim( $key ) ) { 1667 wp_die( __( 'Please provide a custom field name.' ) ); 1668 } 1669 1670 $meta = get_metadata_by_mid( 'post', $meta_id ); 1671 1672 if ( ! $meta ) { 1673 wp_die( 0 ); // If meta doesn't exist. 1674 } 1675 1676 if ( 1677 is_protected_meta( $meta->meta_key, 'post' ) || is_protected_meta( $key, 'post' ) || 1678 ! current_user_can( 'edit_post_meta', $meta->post_id, $meta->meta_key ) || 1679 ! current_user_can( 'edit_post_meta', $meta->post_id, $key ) 1680 ) { 1681 wp_die( -1 ); 1682 } 1683 1684 if ( $meta->meta_value !== $value || $meta->meta_key !== $key ) { 1685 $update_result = update_metadata_by_mid( 'post', $meta_id, $value, $key ); 1686 1687 if ( ! $update_result ) { 1688 wp_die( 0 ); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems). 1689 } 1690 } 1691 1692 $response = new WP_Ajax_Response( 1693 array( 1694 'what' => 'meta', 1695 'id' => $meta_id, 1696 'old_id' => $meta_id, 1697 'data' => _list_meta_row( 1698 array( 1699 'meta_key' => $key, 1700 'meta_value' => $value, 1701 'meta_id' => $meta_id, 1702 ), 1703 $count 1704 ), 1705 'position' => 0, 1706 'supplemental' => array( 'postid' => $meta->post_id ), 1707 ) 1708 ); 1709 } 1710 1711 $response->send(); 1712 } 1713 1714 /** 1715 * Handles adding a user via AJAX. 1716 * 1717 * @since 3.1.0 1718 * 1719 * @param string $action Action to perform. 1720 */ 1721 function wp_ajax_add_user( $action ) { 1722 if ( empty( $action ) ) { 1723 $action = 'add-user'; 1724 } 1725 1726 check_ajax_referer( $action ); 1727 1728 if ( ! current_user_can( 'create_users' ) ) { 1729 wp_die( -1 ); 1730 } 1731 1732 $user_id = edit_user(); 1733 1734 if ( ! $user_id ) { 1735 wp_die( 0 ); 1736 } elseif ( is_wp_error( $user_id ) ) { 1737 $response = new WP_Ajax_Response( 1738 array( 1739 'what' => 'user', 1740 'id' => $user_id, 1741 ) 1742 ); 1743 $response->send(); 1744 } 1745 1746 $user_object = get_userdata( $user_id ); 1747 $wp_list_table = _get_list_table( 'WP_Users_List_Table' ); 1748 1749 $role = current( $user_object->roles ); 1750 1751 $response = new WP_Ajax_Response( 1752 array( 1753 'what' => 'user', 1754 'id' => $user_id, 1755 'data' => $wp_list_table->single_row( $user_object, '', $role ), 1756 'supplemental' => array( 1757 'show-link' => sprintf( 1758 /* translators: %s: The new user. */ 1759 __( 'User %s added' ), 1760 '<a href="#user-' . $user_id . '">' . esc_html( $user_object->user_login ) . '</a>' 1761 ), 1762 'role' => $role, 1763 ), 1764 ) 1765 ); 1766 $response->send(); 1767 } 1768 1769 /** 1770 * Handles closed post boxes via AJAX. 1771 * 1772 * @since 3.1.0 1773 */ 1774 function wp_ajax_closed_postboxes() { 1775 check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' ); 1776 $closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed'] ) : array(); 1777 $closed = array_filter( $closed ); 1778 1779 $hidden = isset( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array(); 1780 $hidden = array_filter( $hidden ); 1781 1782 $page = $_POST['page'] ?? ''; 1783 1784 if ( sanitize_key( $page ) !== $page ) { 1785 wp_die( 0 ); 1786 } 1787 1788 $user = wp_get_current_user(); 1789 if ( ! $user ) { 1790 wp_die( -1 ); 1791 } 1792 1793 if ( is_array( $closed ) ) { 1794 update_user_meta( $user->ID, "closedpostboxes_$page", $closed ); 1795 } 1796 1797 if ( is_array( $hidden ) ) { 1798 // Postboxes that are always shown. 1799 $hidden = array_diff( $hidden, array( 'submitdiv', 'linksubmitdiv', 'manage-menu', 'create-menu' ) ); 1800 update_user_meta( $user->ID, "metaboxhidden_$page", $hidden ); 1801 } 1802 1803 wp_die( 1 ); 1804 } 1805 1806 /** 1807 * Handles hidden columns via AJAX. 1808 * 1809 * @since 3.1.0 1810 */ 1811 function wp_ajax_hidden_columns() { 1812 check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' ); 1813 $page = $_POST['page'] ?? ''; 1814 1815 if ( sanitize_key( $page ) !== $page ) { 1816 wp_die( 0 ); 1817 } 1818 1819 $user = wp_get_current_user(); 1820 if ( ! $user ) { 1821 wp_die( -1 ); 1822 } 1823 1824 $hidden = ! empty( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array(); 1825 update_user_meta( $user->ID, "manage{$page}columnshidden", $hidden ); 1826 1827 wp_die( 1 ); 1828 } 1829 1830 /** 1831 * Handles the meta box reordering setting via Ajax. 1832 * 1833 * @since 7.2.0 1834 */ 1835 function wp_ajax_meta_box_reordering() { 1836 check_ajax_referer( 'screen-options-nonce', 'screenoptionnonce' ); 1837 1838 $enabled = isset( $_POST['enabled'] ) && '1' === (string) $_POST['enabled']; 1839 update_user_option( get_current_user_id(), 'meta_box_reordering', $enabled ? 'enabled' : 'disabled' ); 1840 1841 wp_die( 1 ); 1842 } 1843 1844 /** 1845 * Handles updating whether to display the welcome panel via AJAX. 1846 * 1847 * @since 3.1.0 1848 */ 1849 function wp_ajax_update_welcome_panel() { 1850 check_ajax_referer( 'welcome-panel-nonce', 'welcomepanelnonce' ); 1851 1852 if ( ! current_user_can( 'edit_theme_options' ) ) { 1853 wp_die( -1 ); 1854 } 1855 1856 update_user_meta( get_current_user_id(), 'show_welcome_panel', empty( $_POST['visible'] ) ? 0 : 1 ); 1857 1858 wp_die( 1 ); 1859 } 1860 1861 /** 1862 * Handles for retrieving menu meta boxes via AJAX. 1863 * 1864 * @since 3.1.0 1865 */ 1866 function wp_ajax_menu_get_metabox() { 1867 if ( ! current_user_can( 'edit_theme_options' ) ) { 1868 wp_die( -1 ); 1869 } 1870 1871 require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; 1872 1873 if ( isset( $_POST['item-type'] ) && 'post_type' === $_POST['item-type'] ) { 1874 $type = 'posttype'; 1875 $callback = 'wp_nav_menu_item_post_type_meta_box'; 1876 $items = (array) get_post_types( array( 'show_in_nav_menus' => true ), 'object' ); 1877 } elseif ( isset( $_POST['item-type'] ) && 'taxonomy' === $_POST['item-type'] ) { 1878 $type = 'taxonomy'; 1879 $callback = 'wp_nav_menu_item_taxonomy_meta_box'; 1880 $items = (array) get_taxonomies( array( 'show_ui' => true ), 'object' ); 1881 } 1882 1883 if ( ! empty( $_POST['item-object'] ) && isset( $items[ $_POST['item-object'] ] ) ) { 1884 $menus_meta_box_object = $items[ $_POST['item-object'] ]; 1885 1886 /** This filter is documented in wp-admin/includes/nav-menu.php */ 1887 $item = apply_filters( 'nav_menu_meta_box_object', $menus_meta_box_object ); 1888 1889 $box_args = array( 1890 'id' => 'add-' . $item->name, 1891 'title' => $item->labels->name, 1892 'callback' => $callback, 1893 'args' => $item, 1894 ); 1895 1896 ob_start(); 1897 $callback( null, $box_args ); 1898 1899 $markup = ob_get_clean(); 1900 1901 echo wp_json_encode( 1902 array( 1903 'replace-id' => $type . '-' . $item->name, 1904 'markup' => $markup, 1905 ) 1906 ); 1907 } 1908 1909 wp_die(); 1910 } 1911 1912 /** 1913 * Handles internal linking via AJAX. 1914 * 1915 * @since 3.1.0 1916 */ 1917 function wp_ajax_wp_link_ajax() { 1918 check_ajax_referer( 'internal-linking', '_ajax_linking_nonce' ); 1919 1920 $args = array(); 1921 1922 if ( isset( $_POST['search'] ) ) { 1923 $args['s'] = wp_unslash( $_POST['search'] ); 1924 } 1925 1926 if ( isset( $_POST['term'] ) ) { 1927 $args['s'] = wp_unslash( $_POST['term'] ); 1928 } 1929 1930 $args['pagenum'] = ! empty( $_POST['page'] ) ? absint( $_POST['page'] ) : 1; 1931 1932 if ( ! class_exists( '_WP_Editors', false ) ) { 1933 require ABSPATH . WPINC . '/class-wp-editor.php'; 1934 } 1935 1936 $results = _WP_Editors::wp_link_query( $args ); 1937 1938 if ( ! isset( $results ) ) { 1939 wp_die( 0 ); 1940 } 1941 1942 echo wp_json_encode( $results ); 1943 echo "\n"; 1944 1945 wp_die(); 1946 } 1947 1948 /** 1949 * Handles saving menu locations via AJAX. 1950 * 1951 * @since 3.1.0 1952 */ 1953 function wp_ajax_menu_locations_save() { 1954 if ( ! current_user_can( 'edit_theme_options' ) ) { 1955 wp_die( -1 ); 1956 } 1957 1958 check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' ); 1959 1960 if ( ! isset( $_POST['menu-locations'] ) ) { 1961 wp_die( 0 ); 1962 } 1963 1964 set_theme_mod( 'nav_menu_locations', array_map( 'absint', $_POST['menu-locations'] ) ); 1965 wp_die( 1 ); 1966 } 1967 1968 /** 1969 * Handles saving the meta box order via AJAX. 1970 * 1971 * @since 3.1.0 1972 */ 1973 function wp_ajax_meta_box_order() { 1974 check_ajax_referer( 'meta-box-order' ); 1975 $order = isset( $_POST['order'] ) ? (array) $_POST['order'] : false; 1976 $page_columns = $_POST['page_columns'] ?? 'auto'; 1977 1978 if ( 'auto' !== $page_columns ) { 1979 $page_columns = (int) $page_columns; 1980 } 1981 1982 $page = $_POST['page'] ?? ''; 1983 1984 if ( sanitize_key( $page ) !== $page ) { 1985 wp_die( 0 ); 1986 } 1987 1988 $user = wp_get_current_user(); 1989 if ( ! $user ) { 1990 wp_die( -1 ); 1991 } 1992 1993 if ( $order ) { 1994 update_user_meta( $user->ID, "meta-box-order_$page", $order ); 1995 } 1996 1997 if ( $page_columns ) { 1998 update_user_meta( $user->ID, "screen_layout_$page", $page_columns ); 1999 } 2000 2001 wp_send_json_success(); 2002 } 2003 2004 /** 2005 * Handles menu quick searching via AJAX. 2006 * 2007 * @since 3.1.0 2008 */ 2009 function wp_ajax_menu_quick_search() { 2010 if ( ! current_user_can( 'edit_theme_options' ) ) { 2011 wp_die( -1 ); 2012 } 2013 2014 require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; 2015 2016 _wp_ajax_menu_quick_search( $_POST ); 2017 2018 wp_die(); 2019 } 2020 2021 /** 2022 * Handles retrieving a permalink via AJAX. 2023 * 2024 * @since 3.1.0 2025 */ 2026 function wp_ajax_get_permalink() { 2027 check_ajax_referer( 'getpermalink', 'getpermalinknonce' ); 2028 $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0; 2029 if ( ! $post_id ) { 2030 // Bypass call to get_preview_post_link() for unspecified post ID. 2031 wp_die( '' ); 2032 } 2033 if ( ! current_user_can( 'edit_post', $post_id ) ) { 2034 wp_die( -1 ); 2035 } 2036 wp_die( get_preview_post_link( $post_id ) ); 2037 } 2038 2039 /** 2040 * Handles retrieving a sample permalink via AJAX. 2041 * 2042 * @since 3.1.0 2043 */ 2044 function wp_ajax_sample_permalink() { 2045 check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' ); 2046 $post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0; 2047 if ( ! $post_id ) { 2048 // Bypass call to get_sample_permalink_html() for unspecified post ID. 2049 wp_die( '' ); 2050 } 2051 if ( ! current_user_can( 'edit_post', $post_id ) ) { 2052 wp_die( -1 ); 2053 } 2054 $title = $_POST['new_title'] ?? ''; 2055 $slug = $_POST['new_slug'] ?? null; 2056 wp_die( get_sample_permalink_html( $post_id, $title, $slug ) ); 2057 } 2058 2059 /** 2060 * Handles Quick Edit saving a post from a list table via AJAX. 2061 * 2062 * @since 3.1.0 2063 * 2064 * @global string $mode List table view mode. 2065 */ 2066 function wp_ajax_inline_save() { 2067 global $mode; 2068 2069 check_ajax_referer( 'inlineeditnonce', '_inline_edit' ); 2070 2071 if ( ! isset( $_POST['post_ID'] ) || ! (int) $_POST['post_ID'] ) { 2072 wp_die(); 2073 } 2074 2075 $post_id = (int) $_POST['post_ID']; 2076 2077 if ( 'page' === $_POST['post_type'] ) { 2078 if ( ! current_user_can( 'edit_page', $post_id ) ) { 2079 wp_die( __( 'Sorry, you are not allowed to edit this page.' ) ); 2080 } 2081 } else { 2082 if ( ! current_user_can( 'edit_post', $post_id ) ) { 2083 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); 2084 } 2085 } 2086 2087 $last = wp_check_post_lock( $post_id ); 2088 2089 if ( $last ) { 2090 $last_user = get_userdata( $last ); 2091 $last_user_name = $last_user ? $last_user->display_name : __( 'Someone' ); 2092 2093 /* translators: %s: User's display name. */ 2094 $msg_template = __( 'Saving is disabled: %s is currently editing this post.' ); 2095 2096 if ( 'page' === $_POST['post_type'] ) { 2097 /* translators: %s: User's display name. */ 2098 $msg_template = __( 'Saving is disabled: %s is currently editing this page.' ); 2099 } 2100 2101 printf( $msg_template, esc_html( $last_user_name ) ); 2102 wp_die(); 2103 } 2104 2105 $data = &$_POST; 2106 2107 $post = get_post( $post_id, ARRAY_A ); 2108 if ( ! $post ) { 2109 wp_die(); 2110 } 2111 2112 // Since it's coming from the database. 2113 $post = wp_slash( $post ); 2114 2115 $data['content'] = $post['post_content']; 2116 $data['excerpt'] = $post['post_excerpt']; 2117 2118 // Rename. 2119 $data['user_ID'] = get_current_user_id(); 2120 2121 if ( isset( $data['post_parent'] ) ) { 2122 $data['parent_id'] = $data['post_parent']; 2123 } 2124 2125 // Status. 2126 if ( isset( $data['keep_private'] ) && 'private' === $data['keep_private'] ) { 2127 $data['visibility'] = 'private'; 2128 $data['post_status'] = 'private'; 2129 } elseif ( isset( $data['_status'] ) ) { 2130 $data['post_status'] = $data['_status']; 2131 } 2132 2133 if ( empty( $data['comment_status'] ) ) { 2134 $data['comment_status'] = 'closed'; 2135 } 2136 2137 if ( empty( $data['ping_status'] ) ) { 2138 $data['ping_status'] = 'closed'; 2139 } 2140 2141 // Exclude terms from taxonomies that are not supposed to appear in Quick Edit. 2142 if ( ! empty( $data['tax_input'] ) ) { 2143 foreach ( $data['tax_input'] as $taxonomy => $terms ) { 2144 $tax_object = get_taxonomy( $taxonomy ); 2145 /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */ 2146 if ( ! apply_filters( 'quick_edit_show_taxonomy', $tax_object->show_in_quick_edit, $taxonomy, $post['post_type'] ) ) { 2147 unset( $data['tax_input'][ $taxonomy ] ); 2148 } 2149 } 2150 } 2151 2152 // Hack: wp_unique_post_slug() doesn't work for drafts, so we will fake that our post is published. 2153 if ( ! empty( $data['post_name'] ) && in_array( $post['post_status'], array( 'draft', 'pending' ), true ) ) { 2154 $post['post_status'] = 'publish'; 2155 $data['post_name'] = wp_unique_post_slug( $data['post_name'], $post['ID'], $post['post_status'], $post['post_type'], $post['post_parent'] ); 2156 } 2157 2158 // Update the post. 2159 edit_post(); 2160 2161 $wp_list_table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => $_POST['screen'] ) ); 2162 2163 $mode = 'excerpt' === $_POST['post_view'] ? 'excerpt' : 'list'; 2164 2165 $level = 0; 2166 if ( is_post_type_hierarchical( $wp_list_table->screen->post_type ) ) { 2167 $request_post = array( get_post( $_POST['post_ID'] ) ); 2168 $parent = $request_post[0]->post_parent; 2169 2170 while ( $parent > 0 ) { 2171 $parent_post = get_post( $parent ); 2172 $parent = $parent_post->post_parent; 2173 ++$level; 2174 } 2175 } 2176 2177 $wp_list_table->display_rows( array( get_post( $_POST['post_ID'] ) ), $level ); 2178 2179 wp_die(); 2180 } 2181 2182 /** 2183 * Handles Quick Edit saving for a term via AJAX. 2184 * 2185 * @since 3.1.0 2186 */ 2187 function wp_ajax_inline_save_tax() { 2188 check_ajax_referer( 'taxinlineeditnonce', '_inline_edit' ); 2189 2190 $taxonomy = sanitize_key( $_POST['taxonomy'] ); 2191 $taxonomy_object = get_taxonomy( $taxonomy ); 2192 2193 if ( ! $taxonomy_object ) { 2194 wp_die( 0 ); 2195 } 2196 2197 if ( ! isset( $_POST['tax_ID'] ) || ! (int) $_POST['tax_ID'] ) { 2198 wp_die( -1 ); 2199 } 2200 2201 $id = (int) $_POST['tax_ID']; 2202 2203 if ( ! current_user_can( 'edit_term', $id ) ) { 2204 wp_die( -1 ); 2205 } 2206 2207 $wp_list_table = _get_list_table( 'WP_Terms_List_Table', array( 'screen' => 'edit-' . $taxonomy ) ); 2208 2209 $tag = get_term( $id, $taxonomy ); 2210 $_POST['description'] = $tag->description; 2211 2212 $updated = wp_update_term( $id, $taxonomy, $_POST ); 2213 2214 if ( $updated && ! is_wp_error( $updated ) ) { 2215 $tag = get_term( $updated['term_id'], $taxonomy ); 2216 if ( ! $tag || is_wp_error( $tag ) ) { 2217 if ( is_wp_error( $tag ) && $tag->get_error_message() ) { 2218 wp_die( $tag->get_error_message() ); 2219 } 2220 wp_die( __( 'Item not updated.' ) ); 2221 } 2222 } else { 2223 if ( is_wp_error( $updated ) && $updated->get_error_message() ) { 2224 wp_die( $updated->get_error_message() ); 2225 } 2226 wp_die( __( 'Item not updated.' ) ); 2227 } 2228 2229 $level = 0; 2230 $parent = $tag->parent; 2231 2232 while ( $parent > 0 ) { 2233 $parent_tag = get_term( $parent, $taxonomy ); 2234 $parent = $parent_tag->parent; 2235 ++$level; 2236 } 2237 2238 $wp_list_table->single_row( $tag, $level ); 2239 wp_die(); 2240 } 2241 2242 /** 2243 * Handles querying posts for the Find Posts modal via AJAX. 2244 * 2245 * @see window.findPosts 2246 * 2247 * @since 3.1.0 2248 */ 2249 function wp_ajax_find_posts() { 2250 check_ajax_referer( 'find-posts' ); 2251 2252 $post_types = get_post_types( array( 'public' => true ), 'objects' ); 2253 unset( $post_types['attachment'] ); 2254 2255 $args = array( 2256 'post_type' => array_keys( $post_types ), 2257 'post_status' => 'any', 2258 'posts_per_page' => 50, 2259 ); 2260 2261 $search = wp_unslash( $_POST['ps'] ); 2262 2263 if ( '' !== $search ) { 2264 $args['s'] = $search; 2265 } 2266 2267 $posts = get_posts( $args ); 2268 2269 if ( ! $posts ) { 2270 wp_send_json_error( __( 'No items found.' ) ); 2271 } 2272 2273 $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>'; 2274 $alternate = ''; 2275 foreach ( $posts as $post ) { 2276 $title = trim( $post->post_title ) ? $post->post_title : __( '(no title)' ); 2277 $alternate = ( 'alternate' === $alternate ) ? '' : 'alternate'; 2278 2279 switch ( $post->post_status ) { 2280 case 'publish': 2281 case 'private': 2282 $stat = __( 'Published' ); 2283 break; 2284 case 'future': 2285 $stat = __( 'Scheduled' ); 2286 break; 2287 case 'pending': 2288 $stat = __( 'Pending Review' ); 2289 break; 2290 case 'draft': 2291 $stat = __( 'Draft' ); 2292 break; 2293 } 2294 2295 if ( '0000-00-00 00:00:00' === $post->post_date ) { 2296 $time = ''; 2297 } else { 2298 /* translators: Date format in table columns, see https://www.php.net/manual/datetime.format.php */ 2299 $time = mysql2date( __( 'Y/m/d' ), $post->post_date ); 2300 } 2301 2302 $html .= '<tr class="' . trim( 'found-posts ' . $alternate ) . '"><td class="found-radio"><input type="radio" id="found-' . $post->ID . '" name="found_post_id" value="' . esc_attr( $post->ID ) . '"></td>'; 2303 $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"; 2304 } 2305 2306 $html .= '</tbody></table>'; 2307 2308 wp_send_json_success( $html ); 2309 } 2310 2311 /** 2312 * Handles saving the widgets order via AJAX. 2313 * 2314 * @since 3.1.0 2315 */ 2316 function wp_ajax_widgets_order() { 2317 check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' ); 2318 2319 if ( ! current_user_can( 'edit_theme_options' ) ) { 2320 wp_die( -1 ); 2321 } 2322 2323 unset( $_POST['savewidgets'], $_POST['action'] ); 2324 2325 // Save widgets order for all sidebars. 2326 if ( is_array( $_POST['sidebars'] ) ) { 2327 $sidebars = array(); 2328 2329 foreach ( wp_unslash( $_POST['sidebars'] ) as $key => $val ) { 2330 $sidebar = array(); 2331 2332 if ( ! empty( $val ) ) { 2333 $val = explode( ',', $val ); 2334 2335 foreach ( $val as $k => $v ) { 2336 if ( ! str_contains( $v, 'widget-' ) ) { 2337 continue; 2338 } 2339 2340 $sidebar[ $k ] = substr( $v, strpos( $v, '_' ) + 1 ); 2341 } 2342 } 2343 $sidebars[ $key ] = $sidebar; 2344 } 2345 2346 wp_set_sidebars_widgets( $sidebars ); 2347 wp_die( 1 ); 2348 } 2349 2350 wp_die( -1 ); 2351 } 2352 2353 /** 2354 * Handles saving a widget via AJAX. 2355 * 2356 * @since 3.1.0 2357 * 2358 * @global array $wp_registered_widgets Registered widgets. 2359 * @global array $wp_registered_widget_controls Registered widget controls. 2360 * @global array $wp_registered_widget_updates Registered widget updates. 2361 */ 2362 function wp_ajax_save_widget() { 2363 global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates; 2364 2365 check_ajax_referer( 'save-sidebar-widgets', 'savewidgets' ); 2366 2367 if ( ! current_user_can( 'edit_theme_options' ) || ! isset( $_POST['id_base'] ) ) { 2368 wp_die( -1 ); 2369 } 2370 2371 unset( $_POST['savewidgets'], $_POST['action'] ); 2372 2373 /** 2374 * Fires early when editing the widgets displayed in sidebars. 2375 * 2376 * @since 2.8.0 2377 */ 2378 do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2379 2380 /** 2381 * Fires early when editing the widgets displayed in sidebars. 2382 * 2383 * @since 2.8.0 2384 */ 2385 do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2386 2387 /** This action is documented in wp-admin/widgets-form.php */ 2388 do_action( 'sidebar_admin_setup' ); 2389 2390 $id_base = wp_unslash( $_POST['id_base'] ); 2391 $widget_id = wp_unslash( $_POST['widget-id'] ); 2392 $sidebar_id = $_POST['sidebar']; 2393 $multi_number = ! empty( $_POST['multi_number'] ) ? (int) $_POST['multi_number'] : 0; 2394 $settings = isset( $_POST[ 'widget-' . $id_base ] ) && is_array( $_POST[ 'widget-' . $id_base ] ) ? $_POST[ 'widget-' . $id_base ] : false; 2395 $error = '<p>' . __( 'An error has occurred. Please reload the page and try again.' ) . '</p>'; 2396 2397 $sidebars = wp_get_sidebars_widgets(); 2398 $sidebar = $sidebars[ $sidebar_id ] ?? array(); 2399 2400 // Delete. 2401 if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) { 2402 2403 if ( ! isset( $wp_registered_widgets[ $widget_id ] ) ) { 2404 wp_die( $error ); 2405 } 2406 2407 $sidebar = array_diff( $sidebar, array( $widget_id ) ); 2408 $_POST = array( 2409 'sidebar' => $sidebar_id, 2410 'widget-' . $id_base => array(), 2411 'the-widget-id' => $widget_id, 2412 'delete_widget' => '1', 2413 ); 2414 2415 /** This action is documented in wp-admin/widgets-form.php */ 2416 do_action( 'delete_widget', $widget_id, $sidebar_id, $id_base ); 2417 2418 } elseif ( $settings && preg_match( '/__i__|%i%/', key( $settings ) ) ) { 2419 if ( ! $multi_number ) { 2420 wp_die( $error ); 2421 } 2422 2423 $_POST[ 'widget-' . $id_base ] = array( $multi_number => reset( $settings ) ); 2424 $widget_id = $id_base . '-' . $multi_number; 2425 $sidebar[] = $widget_id; 2426 } 2427 $_POST['widget-id'] = $sidebar; 2428 2429 foreach ( (array) $wp_registered_widget_updates as $name => $control ) { 2430 2431 if ( $name === $id_base ) { 2432 if ( ! is_callable( $control['callback'] ) ) { 2433 continue; 2434 } 2435 2436 ob_start(); 2437 call_user_func_array( $control['callback'], $control['params'] ); 2438 ob_end_clean(); 2439 break; 2440 } 2441 } 2442 2443 if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) { 2444 $sidebars[ $sidebar_id ] = $sidebar; 2445 wp_set_sidebars_widgets( $sidebars ); 2446 echo "deleted:$widget_id"; 2447 wp_die(); 2448 } 2449 2450 if ( ! empty( $_POST['add_new'] ) ) { 2451 wp_die(); 2452 } 2453 2454 $form = $wp_registered_widget_controls[ $widget_id ]; 2455 if ( $form ) { 2456 call_user_func_array( $form['callback'], $form['params'] ); 2457 } 2458 2459 wp_die(); 2460 } 2461 2462 /** 2463 * Handles updating a widget via AJAX. 2464 * 2465 * @since 3.9.0 2466 * 2467 * @global WP_Customize_Manager $wp_customize Customizer manager object. 2468 */ 2469 function wp_ajax_update_widget() { 2470 global $wp_customize; 2471 $wp_customize->widgets->wp_ajax_update_widget(); 2472 } 2473 2474 /** 2475 * Handles removing inactive widgets via AJAX. 2476 * 2477 * @since 4.4.0 2478 */ 2479 function wp_ajax_delete_inactive_widgets() { 2480 check_ajax_referer( 'remove-inactive-widgets', 'removeinactivewidgets' ); 2481 2482 if ( ! current_user_can( 'edit_theme_options' ) ) { 2483 wp_die( -1 ); 2484 } 2485 2486 unset( $_POST['removeinactivewidgets'], $_POST['action'] ); 2487 /** This action is documented in wp-admin/includes/ajax-actions.php */ 2488 do_action( 'load-widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2489 /** This action is documented in wp-admin/includes/ajax-actions.php */ 2490 do_action( 'widgets.php' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores 2491 /** This action is documented in wp-admin/widgets-form.php */ 2492 do_action( 'sidebar_admin_setup' ); 2493 2494 require_once ABSPATH . 'wp-admin/includes/widgets.php'; 2495 wp_delete_inactive_widgets(); 2496 2497 wp_die(); 2498 } 2499 2500 /** 2501 * Handles creating missing image sub-sizes for just uploaded images via AJAX. 2502 * 2503 * @since 5.3.0 2504 */ 2505 function wp_ajax_media_create_image_subsizes() { 2506 check_ajax_referer( 'media-form' ); 2507 2508 if ( ! current_user_can( 'upload_files' ) ) { 2509 wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to upload files.' ) ) ); 2510 } 2511 2512 if ( empty( $_POST['attachment_id'] ) ) { 2513 wp_send_json_error( array( 'message' => __( 'Upload failed. Please reload and try again.' ) ) ); 2514 } 2515 2516 $attachment_id = (int) $_POST['attachment_id']; 2517 2518 if ( ! empty( $_POST['_wp_upload_failed_cleanup'] ) ) { 2519 // Upload failed. Cleanup. 2520 if ( wp_attachment_is_image( $attachment_id ) && current_user_can( 'delete_post', $attachment_id ) ) { 2521 $attachment = get_post( $attachment_id ); 2522 2523 // Created at most 10 min ago. 2524 if ( $attachment && ( time() - strtotime( $attachment->post_date_gmt ) < 600 ) ) { 2525 wp_delete_attachment( $attachment_id, true ); 2526 wp_send_json_success(); 2527 } 2528 } 2529 } 2530 2531 /* 2532 * Set a custom header with the attachment_id. 2533 * Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. 2534 */ 2535 if ( ! headers_sent() ) { 2536 header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id ); 2537 } 2538 2539 /* 2540 * This can still be pretty slow and cause timeout or out of memory errors. 2541 * The js that handles the response would need to also handle HTTP 500 errors. 2542 */ 2543 wp_update_image_subsizes( $attachment_id ); 2544 2545 if ( ! empty( $_POST['_legacy_support'] ) ) { 2546 // The old (inline) uploader. Only needs the attachment_id. 2547 $response = array( 'id' => $attachment_id ); 2548 } else { 2549 // Media modal and Media Library grid view. 2550 $response = wp_prepare_attachment_for_js( $attachment_id ); 2551 2552 if ( ! $response ) { 2553 wp_send_json_error( array( 'message' => __( 'Upload failed.' ) ) ); 2554 } 2555 } 2556 2557 // At this point the image has been uploaded successfully. 2558 wp_send_json_success( $response ); 2559 } 2560 2561 /** 2562 * Handles uploading attachments via AJAX. 2563 * 2564 * @since 3.3.0 2565 */ 2566 function wp_ajax_upload_attachment() { 2567 check_ajax_referer( 'media-form' ); 2568 /* 2569 * This function does not use wp_send_json_success() / wp_send_json_error() 2570 * as the html4 Plupload handler requires a text/html Content-Type for older IE. 2571 * See https://core.trac.wordpress.org/ticket/31037 2572 */ 2573 2574 if ( ! current_user_can( 'upload_files' ) ) { 2575 echo wp_json_encode( 2576 array( 2577 'success' => false, 2578 'data' => array( 2579 'message' => __( 'Sorry, you are not allowed to upload files.' ), 2580 'filename' => esc_html( $_FILES['async-upload']['name'] ), 2581 ), 2582 ) 2583 ); 2584 2585 wp_die(); 2586 } 2587 2588 if ( isset( $_REQUEST['post_id'] ) ) { 2589 $post_id = $_REQUEST['post_id']; 2590 2591 if ( ! current_user_can( 'edit_post', $post_id ) ) { 2592 echo wp_json_encode( 2593 array( 2594 'success' => false, 2595 'data' => array( 2596 'message' => __( 'Sorry, you are not allowed to attach files to this post.' ), 2597 'filename' => esc_html( $_FILES['async-upload']['name'] ), 2598 ), 2599 ) 2600 ); 2601 2602 wp_die(); 2603 } 2604 } else { 2605 $post_id = null; 2606 } 2607 2608 $post_data = ! empty( $_REQUEST['post_data'] ) ? _wp_get_allowed_postdata( _wp_translate_postdata( false, (array) $_REQUEST['post_data'] ) ) : array(); 2609 2610 if ( is_wp_error( $post_data ) ) { 2611 wp_die( $post_data->get_error_message() ); 2612 } 2613 2614 // If the context is custom header or background, make sure the uploaded file is an image. 2615 if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ), true ) ) { 2616 $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'] ); 2617 2618 if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { 2619 echo wp_json_encode( 2620 array( 2621 'success' => false, 2622 'data' => array( 2623 'message' => __( 'The uploaded file is not a valid image. Please try again.' ), 2624 'filename' => esc_html( $_FILES['async-upload']['name'] ), 2625 ), 2626 ) 2627 ); 2628 2629 wp_die(); 2630 } 2631 } 2632 2633 $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data ); 2634 2635 if ( is_wp_error( $attachment_id ) ) { 2636 echo wp_json_encode( 2637 array( 2638 'success' => false, 2639 'data' => array( 2640 'message' => $attachment_id->get_error_message(), 2641 'filename' => esc_html( $_FILES['async-upload']['name'] ), 2642 ), 2643 ) 2644 ); 2645 2646 wp_die(); 2647 } 2648 2649 if ( isset( $post_data['context'] ) && isset( $post_data['theme'] ) ) { 2650 if ( 'custom-background' === $post_data['context'] ) { 2651 update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', $post_data['theme'] ); 2652 } 2653 2654 if ( 'custom-header' === $post_data['context'] ) { 2655 update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', $post_data['theme'] ); 2656 } 2657 } 2658 2659 $attachment = wp_prepare_attachment_for_js( $attachment_id ); 2660 if ( ! $attachment ) { 2661 wp_die(); 2662 } 2663 2664 echo wp_json_encode( 2665 array( 2666 'success' => true, 2667 'data' => $attachment, 2668 ) 2669 ); 2670 2671 wp_die(); 2672 } 2673 2674 /** 2675 * Handles image editing via AJAX. 2676 * 2677 * @since 3.1.0 2678 * 2679 * @return never 2680 */ 2681 function wp_ajax_image_editor() { 2682 $attachment_id = (int) $_POST['postid']; 2683 2684 if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) { 2685 wp_die( -1 ); 2686 } 2687 2688 check_ajax_referer( "image_editor-$attachment_id" ); 2689 require_once ABSPATH . 'wp-admin/includes/image-edit.php'; 2690 2691 $message = false; 2692 2693 switch ( $_POST['do'] ) { 2694 case 'save': 2695 $message = wp_save_image( $attachment_id ); 2696 if ( ! empty( $message->error ) ) { 2697 wp_send_json_error( $message ); 2698 } 2699 2700 wp_send_json_success( $message ); 2701 break; // @phpstan-ignore deadCode.unreachable (Ends the case so that it does not read as falling through, which PHPCS reports.) 2702 case 'scale': 2703 $message = wp_save_image( $attachment_id ); 2704 break; 2705 case 'restore': 2706 $message = wp_restore_image( $attachment_id ); 2707 break; 2708 } 2709 2710 ob_start(); 2711 wp_image_editor( $attachment_id, $message ); 2712 $html = ob_get_clean(); 2713 2714 if ( ! empty( $message->error ) ) { 2715 wp_send_json_error( 2716 array( 2717 'message' => $message, 2718 'html' => $html, 2719 ) 2720 ); 2721 } 2722 2723 wp_send_json_success( 2724 array( 2725 'message' => $message, 2726 'html' => $html, 2727 ) 2728 ); 2729 } 2730 2731 /** 2732 * Handles setting the featured image via AJAX. 2733 * 2734 * @since 3.1.0 2735 */ 2736 function wp_ajax_set_post_thumbnail() { 2737 $json = ! empty( $_REQUEST['json'] ); // New-style request. 2738 2739 $post_id = (int) $_POST['post_id']; 2740 if ( ! current_user_can( 'edit_post', $post_id ) ) { 2741 wp_die( -1 ); 2742 } 2743 2744 $thumbnail_id = (int) $_POST['thumbnail_id']; 2745 2746 if ( $json ) { 2747 check_ajax_referer( "update-post_$post_id" ); 2748 } else { 2749 check_ajax_referer( "set_post_thumbnail-$post_id" ); 2750 } 2751 2752 if ( -1 === $thumbnail_id ) { 2753 if ( delete_post_thumbnail( $post_id ) ) { 2754 $return = _wp_post_thumbnail_html( null, $post_id ); 2755 $json ? wp_send_json_success( $return ) : wp_die( $return ); 2756 } else { 2757 wp_die( 0 ); 2758 } 2759 } 2760 2761 if ( set_post_thumbnail( $post_id, $thumbnail_id ) ) { 2762 $return = _wp_post_thumbnail_html( $thumbnail_id, $post_id ); 2763 $json ? wp_send_json_success( $return ) : wp_die( $return ); 2764 } 2765 2766 wp_die( 0 ); 2767 } 2768 2769 /** 2770 * Handles retrieving HTML for the featured image via AJAX. 2771 * 2772 * @since 4.6.0 2773 */ 2774 function wp_ajax_get_post_thumbnail_html() { 2775 $post_id = (int) $_POST['post_id']; 2776 2777 check_ajax_referer( "update-post_$post_id" ); 2778 2779 if ( ! current_user_can( 'edit_post', $post_id ) ) { 2780 wp_die( -1 ); 2781 } 2782 2783 $thumbnail_id = (int) $_POST['thumbnail_id']; 2784 2785 // For backward compatibility, -1 refers to no featured image. 2786 if ( -1 === $thumbnail_id ) { 2787 $thumbnail_id = null; 2788 } 2789 2790 $return = _wp_post_thumbnail_html( $thumbnail_id, $post_id ); 2791 wp_send_json_success( $return ); 2792 } 2793 2794 /** 2795 * Handles setting the featured image for an attachment via AJAX. 2796 * 2797 * @since 4.0.0 2798 * 2799 * @see set_post_thumbnail() 2800 * 2801 * @return never 2802 */ 2803 function wp_ajax_set_attachment_thumbnail() { 2804 if ( empty( $_POST['urls'] ) || ! is_array( $_POST['urls'] ) ) { 2805 wp_send_json_error(); 2806 } 2807 2808 $thumbnail_id = (int) $_POST['thumbnail_id']; 2809 if ( empty( $thumbnail_id ) ) { 2810 wp_send_json_error(); 2811 } 2812 2813 if ( false === check_ajax_referer( 'set-attachment-thumbnail', '_ajax_nonce', false ) ) { 2814 wp_send_json_error(); 2815 } 2816 2817 $post_ids = array(); 2818 // For each URL, try to find its corresponding post ID. 2819 foreach ( $_POST['urls'] as $url ) { 2820 $post_id = attachment_url_to_postid( $url ); 2821 if ( ! empty( $post_id ) ) { 2822 $post_ids[] = $post_id; 2823 } 2824 } 2825 2826 if ( empty( $post_ids ) ) { 2827 wp_send_json_error(); 2828 } 2829 2830 $success = 0; 2831 // For each found attachment, set its thumbnail. 2832 foreach ( $post_ids as $post_id ) { 2833 if ( ! current_user_can( 'edit_post', $post_id ) ) { 2834 continue; 2835 } 2836 2837 if ( set_post_thumbnail( $post_id, $thumbnail_id ) ) { 2838 ++$success; 2839 } 2840 } 2841 2842 if ( 0 === $success ) { 2843 wp_send_json_error(); 2844 } else { 2845 wp_send_json_success(); 2846 } 2847 } 2848 2849 /** 2850 * Handles formatting a date via AJAX. 2851 * 2852 * @since 3.1.0 2853 */ 2854 function wp_ajax_date_format() { 2855 wp_die( date_i18n( sanitize_option( 'date_format', wp_unslash( $_POST['date'] ) ) ) ); 2856 } 2857 2858 /** 2859 * Handles formatting a time via AJAX. 2860 * 2861 * @since 3.1.0 2862 */ 2863 function wp_ajax_time_format() { 2864 wp_die( date_i18n( sanitize_option( 'time_format', wp_unslash( $_POST['date'] ) ) ) ); 2865 } 2866 2867 /** 2868 * Handles saving posts from the fullscreen editor via AJAX. 2869 * 2870 * @since 3.1.0 2871 * @deprecated 4.3.0 2872 */ 2873 function wp_ajax_wp_fullscreen_save_post() { 2874 $post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0; 2875 2876 $post = null; 2877 2878 if ( $post_id ) { 2879 $post = get_post( $post_id ); 2880 } 2881 2882 check_ajax_referer( 'update-post_' . $post_id, '_wpnonce' ); 2883 2884 $post_id = edit_post(); 2885 2886 if ( is_wp_error( $post_id ) ) { 2887 wp_send_json_error(); 2888 } 2889 2890 if ( $post ) { 2891 $last_date = mysql2date( __( 'F j, Y' ), $post->post_modified ); 2892 $last_time = mysql2date( __( 'g:i a' ), $post->post_modified ); 2893 } else { 2894 $last_date = date_i18n( __( 'F j, Y' ) ); 2895 $last_time = date_i18n( __( 'g:i a' ) ); 2896 } 2897 2898 $last_id = get_post_meta( $post_id, '_edit_last', true ); 2899 if ( $last_id ) { 2900 $last_user = get_userdata( $last_id ); 2901 /* translators: 1: User's display name, 2: Date of last edit, 3: Time of last edit. */ 2902 $last_edited = sprintf( __( 'Last edited by %1$s on %2$s at %3$s' ), esc_html( $last_user->display_name ), $last_date, $last_time ); 2903 } else { 2904 /* translators: 1: Date of last edit, 2: Time of last edit. */ 2905 $last_edited = sprintf( __( 'Last edited on %1$s at %2$s' ), $last_date, $last_time ); 2906 } 2907 2908 wp_send_json_success( array( 'last_edited' => $last_edited ) ); 2909 } 2910 2911 /** 2912 * Handles removing a post lock via AJAX. 2913 * 2914 * @since 3.1.0 2915 */ 2916 function wp_ajax_wp_remove_post_lock() { 2917 if ( empty( $_POST['post_ID'] ) || empty( $_POST['active_post_lock'] ) ) { 2918 wp_die( 0 ); 2919 } 2920 2921 $post_id = (int) $_POST['post_ID']; 2922 $post = get_post( $post_id ); 2923 2924 if ( ! $post ) { 2925 wp_die( 0 ); 2926 } 2927 2928 check_ajax_referer( 'update-post_' . $post_id ); 2929 2930 if ( ! current_user_can( 'edit_post', $post_id ) ) { 2931 wp_die( -1 ); 2932 } 2933 2934 $active_lock = array_map( 'absint', explode( ':', $_POST['active_post_lock'] ) ); 2935 2936 if ( get_current_user_id() !== $active_lock[1] ) { 2937 wp_die( 0 ); 2938 } 2939 2940 /** 2941 * Filters the post lock window duration. 2942 * 2943 * @since 3.3.0 2944 * 2945 * @param int $interval The interval in seconds the post lock duration 2946 * should last, plus 5 seconds. Default 150. 2947 */ 2948 $new_lock = ( time() - apply_filters( 'wp_check_post_lock_window', 150 ) + 5 ) . ':' . $active_lock[1]; 2949 update_post_meta( $post_id, '_edit_lock', $new_lock, implode( ':', $active_lock ) ); 2950 wp_die( 1 ); 2951 } 2952 2953 /** 2954 * Handles dismissing a WordPress pointer via AJAX. 2955 * 2956 * @since 3.1.0 2957 */ 2958 function wp_ajax_dismiss_wp_pointer() { 2959 $pointer = $_POST['pointer']; 2960 2961 if ( sanitize_key( $pointer ) !== $pointer ) { 2962 wp_die( 0 ); 2963 } 2964 2965 // check_ajax_referer( 'dismiss-pointer_' . $pointer ); 2966 2967 $dismissed = array_filter( explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) ) ); 2968 2969 if ( in_array( $pointer, $dismissed, true ) ) { 2970 wp_die( 0 ); 2971 } 2972 2973 $dismissed[] = $pointer; 2974 $dismissed = implode( ',', $dismissed ); 2975 2976 update_user_meta( get_current_user_id(), 'dismissed_wp_pointers', $dismissed ); 2977 wp_die( 1 ); 2978 } 2979 2980 /** 2981 * Handles getting an attachment via AJAX. 2982 * 2983 * @since 3.5.0 2984 */ 2985 function wp_ajax_get_attachment() { 2986 if ( ! isset( $_REQUEST['id'] ) ) { 2987 wp_send_json_error(); 2988 } 2989 2990 $id = absint( $_REQUEST['id'] ); 2991 if ( ! $id ) { 2992 wp_send_json_error(); 2993 } 2994 2995 $post = get_post( $id ); 2996 if ( ! $post ) { 2997 wp_send_json_error(); 2998 } 2999 3000 if ( 'attachment' !== $post->post_type ) { 3001 wp_send_json_error(); 3002 } 3003 3004 if ( ! current_user_can( 'upload_files' ) ) { 3005 wp_send_json_error(); 3006 } 3007 3008 $attachment = wp_prepare_attachment_for_js( $id ); 3009 if ( ! $attachment ) { 3010 wp_send_json_error(); 3011 } 3012 3013 wp_send_json_success( $attachment ); 3014 } 3015 3016 /** 3017 * Handles querying attachments via AJAX. 3018 * 3019 * @since 3.5.0 3020 */ 3021 function wp_ajax_query_attachments() { 3022 if ( ! current_user_can( 'upload_files' ) ) { 3023 wp_send_json_error(); 3024 } 3025 3026 $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array(); 3027 $keys = array( 3028 's', 3029 'order', 3030 'orderby', 3031 'posts_per_page', 3032 'paged', 3033 'post_mime_type', 3034 'post_parent', 3035 'author', 3036 'post__in', 3037 'post__not_in', 3038 'year', 3039 'monthnum', 3040 ); 3041 3042 foreach ( get_taxonomies_for_attachments( 'objects' ) as $taxonomy ) { 3043 if ( $taxonomy->query_var && isset( $query[ $taxonomy->query_var ] ) ) { 3044 $keys[] = $taxonomy->query_var; 3045 } 3046 } 3047 3048 $query = array_intersect_key( $query, array_flip( $keys ) ); 3049 $query['post_type'] = 'attachment'; 3050 3051 if ( 3052 MEDIA_TRASH && 3053 ! empty( $_REQUEST['query']['post_status'] ) && 3054 'trash' === $_REQUEST['query']['post_status'] 3055 ) { 3056 $query['post_status'] = 'trash'; 3057 } else { 3058 $query['post_status'] = 'inherit'; 3059 } 3060 3061 if ( current_user_can( get_post_type_object( 'attachment' )->cap->read_private_posts ) ) { 3062 $query['post_status'] .= ',private'; 3063 } 3064 3065 // Filter query clauses to include filenames. 3066 if ( isset( $query['s'] ) ) { 3067 add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' ); 3068 } 3069 3070 /** 3071 * Filters the arguments passed to WP_Query during an Ajax 3072 * call for querying attachments. 3073 * 3074 * @since 3.7.0 3075 * 3076 * @see WP_Query::parse_query() 3077 * 3078 * @param array $query An array of query variables. 3079 */ 3080 $query = apply_filters( 'ajax_query_attachments_args', $query ); 3081 $attachments_query = new WP_Query( $query ); 3082 update_post_parent_caches( $attachments_query->posts ); 3083 3084 $posts = array_map( 'wp_prepare_attachment_for_js', $attachments_query->posts ); 3085 $posts = array_filter( $posts ); 3086 $total_posts = $attachments_query->found_posts; 3087 3088 if ( $total_posts < 1 ) { 3089 // Out-of-bounds, run the query again without LIMIT for total count. 3090 unset( $query['paged'] ); 3091 3092 $count_query = new WP_Query(); 3093 $count_query->query( $query ); 3094 $total_posts = $count_query->found_posts; 3095 } 3096 3097 $posts_per_page = (int) $attachments_query->get( 'posts_per_page' ); 3098 3099 $max_pages = $posts_per_page ? (int) ceil( $total_posts / $posts_per_page ) : 0; 3100 3101 header( 'X-WP-Total: ' . (int) $total_posts ); 3102 header( 'X-WP-TotalPages: ' . $max_pages ); 3103 3104 wp_send_json_success( $posts ); 3105 } 3106 3107 /** 3108 * Handles updating attachment attributes via AJAX. 3109 * 3110 * @since 3.5.0 3111 */ 3112 function wp_ajax_save_attachment() { 3113 if ( ! isset( $_REQUEST['id'] ) || ! isset( $_REQUEST['changes'] ) ) { 3114 wp_send_json_error(); 3115 } 3116 3117 $id = absint( $_REQUEST['id'] ); 3118 if ( ! $id ) { 3119 wp_send_json_error(); 3120 } 3121 3122 check_ajax_referer( 'update-post_' . $id, 'nonce' ); 3123 3124 if ( ! current_user_can( 'edit_post', $id ) ) { 3125 wp_send_json_error(); 3126 } 3127 3128 $changes = $_REQUEST['changes']; 3129 $post = get_post( $id, ARRAY_A ); 3130 if ( ! $post ) { 3131 wp_send_json_error(); 3132 } 3133 3134 if ( 'attachment' !== $post['post_type'] ) { 3135 wp_send_json_error(); 3136 } 3137 3138 if ( isset( $changes['parent'] ) ) { 3139 $post['post_parent'] = $changes['parent']; 3140 } 3141 3142 if ( isset( $changes['title'] ) ) { 3143 $post['post_title'] = $changes['title']; 3144 } 3145 3146 if ( isset( $changes['caption'] ) ) { 3147 $post['post_excerpt'] = $changes['caption']; 3148 } 3149 3150 if ( isset( $changes['description'] ) ) { 3151 $post['post_content'] = $changes['description']; 3152 } 3153 3154 if ( MEDIA_TRASH && isset( $changes['status'] ) ) { 3155 $post['post_status'] = $changes['status']; 3156 } 3157 3158 if ( isset( $changes['alt'] ) ) { 3159 $alt = wp_unslash( $changes['alt'] ); 3160 if ( get_post_meta( $id, '_wp_attachment_image_alt', true ) !== $alt ) { 3161 $alt = wp_strip_all_tags( $alt, true ); 3162 update_post_meta( $id, '_wp_attachment_image_alt', wp_slash( $alt ) ); 3163 } 3164 } 3165 3166 if ( wp_attachment_is( 'audio', $post['ID'] ) ) { 3167 $changed = false; 3168 $id3_data = wp_get_attachment_metadata( $post['ID'] ); 3169 3170 if ( ! is_array( $id3_data ) ) { 3171 $changed = true; 3172 $id3_data = array(); 3173 } 3174 3175 foreach ( wp_get_attachment_id3_keys( (object) $post, 'edit' ) as $key => $label ) { 3176 if ( isset( $changes[ $key ] ) ) { 3177 $changed = true; 3178 $id3_data[ $key ] = sanitize_text_field( wp_unslash( $changes[ $key ] ) ); 3179 } 3180 } 3181 3182 if ( $changed ) { 3183 wp_update_attachment_metadata( $id, $id3_data ); 3184 } 3185 } 3186 3187 if ( MEDIA_TRASH && isset( $changes['status'] ) && 'trash' === $changes['status'] ) { 3188 wp_delete_post( $id ); 3189 } else { 3190 wp_update_post( $post ); 3191 } 3192 3193 wp_send_json_success(); 3194 } 3195 3196 /** 3197 * Handles saving backward compatible attachment attributes via AJAX. 3198 * 3199 * @since 3.5.0 3200 */ 3201 function wp_ajax_save_attachment_compat() { 3202 if ( ! isset( $_REQUEST['id'] ) ) { 3203 wp_send_json_error(); 3204 } 3205 3206 $id = absint( $_REQUEST['id'] ); 3207 if ( ! $id ) { 3208 wp_send_json_error(); 3209 } 3210 3211 if ( empty( $_REQUEST['attachments'] ) || empty( $_REQUEST['attachments'][ $id ] ) ) { 3212 wp_send_json_error(); 3213 } 3214 3215 $attachment_data = $_REQUEST['attachments'][ $id ]; 3216 3217 check_ajax_referer( 'update-post_' . $id, 'nonce' ); 3218 3219 if ( ! current_user_can( 'edit_post', $id ) ) { 3220 wp_send_json_error(); 3221 } 3222 3223 $post = get_post( $id, ARRAY_A ); 3224 if ( ! $post ) { 3225 wp_send_json_error(); 3226 } 3227 3228 if ( 'attachment' !== $post['post_type'] ) { 3229 wp_send_json_error(); 3230 } 3231 3232 /** This filter is documented in wp-admin/includes/media.php */ 3233 $post = apply_filters( 'attachment_fields_to_save', $post, $attachment_data ); 3234 3235 if ( isset( $post['errors'] ) ) { 3236 $errors = $post['errors']; // @todo return me and display me! 3237 unset( $post['errors'] ); 3238 } 3239 3240 wp_update_post( $post ); 3241 3242 foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) { 3243 if ( isset( $attachment_data[ $taxonomy ] ) ) { 3244 wp_set_object_terms( $id, array_map( 'trim', preg_split( '/,+/', $attachment_data[ $taxonomy ] ) ), $taxonomy, false ); 3245 } 3246 } 3247 3248 $attachment = wp_prepare_attachment_for_js( $id ); 3249 3250 if ( ! $attachment ) { 3251 wp_send_json_error(); 3252 } 3253 3254 wp_send_json_success( $attachment ); 3255 } 3256 3257 /** 3258 * Handles saving the attachment order via AJAX. 3259 * 3260 * @since 3.5.0 3261 */ 3262 function wp_ajax_save_attachment_order() { 3263 if ( ! isset( $_REQUEST['post_id'] ) ) { 3264 wp_send_json_error(); 3265 } 3266 3267 $post_id = absint( $_REQUEST['post_id'] ); 3268 if ( ! $post_id ) { 3269 wp_send_json_error(); 3270 } 3271 3272 if ( empty( $_REQUEST['attachments'] ) ) { 3273 wp_send_json_error(); 3274 } 3275 3276 check_ajax_referer( 'update-post_' . $post_id, 'nonce' ); 3277 3278 $attachments = $_REQUEST['attachments']; 3279 3280 if ( ! current_user_can( 'edit_post', $post_id ) ) { 3281 wp_send_json_error(); 3282 } 3283 3284 foreach ( $attachments as $attachment_id => $menu_order ) { 3285 if ( ! current_user_can( 'edit_post', $attachment_id ) ) { 3286 continue; 3287 } 3288 3289 $attachment = get_post( $attachment_id ); 3290 3291 if ( ! $attachment ) { 3292 continue; 3293 } 3294 3295 if ( 'attachment' !== $attachment->post_type ) { 3296 continue; 3297 } 3298 3299 wp_update_post( 3300 array( 3301 'ID' => $attachment_id, 3302 'menu_order' => $menu_order, 3303 ) 3304 ); 3305 } 3306 3307 wp_send_json_success(); 3308 } 3309 3310 /** 3311 * Handles sending an attachment to the editor via AJAX. 3312 * 3313 * Generates the HTML to send an attachment to the editor. 3314 * Backward compatible with the {@see 'media_send_to_editor'} filter 3315 * and the chain of filters that follow. 3316 * 3317 * @since 3.5.0 3318 */ 3319 function wp_ajax_send_attachment_to_editor() { 3320 check_ajax_referer( 'media-send-to-editor', 'nonce' ); 3321 3322 $attachment = wp_unslash( $_POST['attachment'] ); 3323 3324 $id = (int) $attachment['id']; 3325 3326 $post = get_post( $id ); 3327 if ( ! $post ) { 3328 wp_send_json_error(); 3329 } 3330 3331 if ( 'attachment' !== $post->post_type ) { 3332 wp_send_json_error(); 3333 } 3334 3335 if ( current_user_can( 'edit_post', $id ) ) { 3336 // If this attachment is unattached, attach it. Primarily a back compat thing. 3337 $insert_into_post_id = (int) $_POST['post_id']; 3338 3339 if ( 0 === $post->post_parent && $insert_into_post_id ) { 3340 wp_update_post( 3341 array( 3342 'ID' => $id, 3343 'post_parent' => $insert_into_post_id, 3344 ) 3345 ); 3346 } 3347 } 3348 3349 $url = empty( $attachment['url'] ) ? '' : $attachment['url']; 3350 $rel = ( str_contains( $url, 'attachment_id' ) || get_attachment_link( $id ) === $url ); 3351 3352 remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' ); 3353 3354 if ( str_starts_with( $post->post_mime_type, 'image' ) ) { 3355 $align = $attachment['align'] ?? 'none'; 3356 $size = $attachment['image-size'] ?? 'medium'; 3357 $alt = $attachment['image_alt'] ?? ''; 3358 3359 // No whitespace-only captions. 3360 $caption = $attachment['post_excerpt'] ?? ''; 3361 if ( '' === trim( $caption ) ) { 3362 $caption = ''; 3363 } 3364 3365 $title = ''; // We no longer insert title tags into <img> tags, as they are redundant. 3366 $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ); 3367 } elseif ( wp_attachment_is( 'video', $post ) || wp_attachment_is( 'audio', $post ) ) { 3368 $html = stripslashes_deep( $_POST['html'] ); 3369 } else { 3370 $html = $attachment['post_title'] ?? ''; 3371 $rel = $rel ? ' rel="attachment wp-att-' . $id . '"' : ''; // Hard-coded string, $id is already sanitized. 3372 3373 if ( ! empty( $url ) ) { 3374 $html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>'; 3375 } 3376 } 3377 3378 /** This filter is documented in wp-admin/includes/media.php */ 3379 $html = apply_filters( 'media_send_to_editor', $html, $id, $attachment ); 3380 3381 wp_send_json_success( $html ); 3382 } 3383 3384 /** 3385 * Handles sending a link to the editor via AJAX. 3386 * 3387 * Generates the HTML to send a non-image embed link to the editor. 3388 * 3389 * Backward compatible with the following filters: 3390 * - file_send_to_editor_url 3391 * - audio_send_to_editor_url 3392 * - video_send_to_editor_url 3393 * 3394 * @since 3.5.0 3395 * 3396 * @global WP_Post $post Global post object. 3397 * @global WP_Embed $wp_embed WordPress Embed object. 3398 */ 3399 function wp_ajax_send_link_to_editor() { 3400 global $post, $wp_embed; 3401 3402 check_ajax_referer( 'media-send-to-editor', 'nonce' ); 3403 3404 $src = wp_unslash( $_POST['src'] ); 3405 if ( ! $src ) { 3406 wp_send_json_error(); 3407 } 3408 3409 if ( ! strpos( $src, '://' ) ) { 3410 $src = 'http://' . $src; 3411 } 3412 3413 $src = sanitize_url( $src ); 3414 if ( ! $src ) { 3415 wp_send_json_error(); 3416 } 3417 3418 $link_text = trim( wp_unslash( $_POST['link_text'] ) ); 3419 if ( ! $link_text ) { 3420 $link_text = wp_basename( $src ); 3421 } 3422 3423 $post = get_post( $_POST['post_id'] ?? 0 ); 3424 3425 // Ping WordPress for an embed. 3426 $check_embed = $wp_embed->run_shortcode( '[embed]' . $src . '[/embed]' ); 3427 3428 // Fallback that WordPress creates when no oEmbed was found. 3429 $fallback = $wp_embed->maybe_make_link( $src ); 3430 3431 if ( $check_embed !== $fallback ) { 3432 // TinyMCE view for [embed] will parse this. 3433 $html = '[embed]' . $src . '[/embed]'; 3434 } elseif ( $link_text ) { 3435 $html = '<a href="' . esc_url( $src ) . '">' . $link_text . '</a>'; 3436 } else { 3437 $html = ''; 3438 } 3439 3440 // Figure out what filter to run: 3441 $type = 'file'; 3442 $extension = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ); 3443 if ( $extension ) { 3444 $extension_type = wp_ext2type( $extension ); 3445 if ( 'audio' === $extension_type || 'video' === $extension_type ) { 3446 $type = $extension_type; 3447 } 3448 } 3449 3450 /** This filter is documented in wp-admin/includes/media.php */ 3451 $html = apply_filters( "{$type}_send_to_editor_url", $html, $src, $link_text ); 3452 3453 wp_send_json_success( $html ); 3454 } 3455 3456 /** 3457 * Handles the Heartbeat API via AJAX. 3458 * 3459 * Runs when the user is logged in. 3460 * 3461 * @since 3.6.0 3462 */ 3463 function wp_ajax_heartbeat() { 3464 if ( empty( $_POST['_nonce'] ) ) { 3465 wp_send_json_error(); 3466 } 3467 3468 $response = array(); 3469 $data = array(); 3470 $nonce_state = wp_verify_nonce( $_POST['_nonce'], 'heartbeat-nonce' ); 3471 3472 // 'screen_id' is the same as $current_screen->id and the JS global 'pagenow'. 3473 if ( ! empty( $_POST['screen_id'] ) ) { 3474 $screen_id = sanitize_key( $_POST['screen_id'] ); 3475 } else { 3476 $screen_id = 'front'; 3477 } 3478 3479 if ( ! empty( $_POST['data'] ) ) { 3480 $data = wp_unslash( (array) $_POST['data'] ); 3481 } 3482 3483 if ( 1 !== $nonce_state ) { 3484 /** 3485 * Filters the nonces to send to the New/Edit Post screen. 3486 * 3487 * @since 4.3.0 3488 * 3489 * @param array $response The Heartbeat response. 3490 * @param array $data The $_POST data sent. 3491 * @param string $screen_id The screen ID. 3492 */ 3493 $response = apply_filters( 'wp_refresh_nonces', $response, $data, $screen_id ); 3494 3495 if ( false === $nonce_state ) { 3496 // User is logged in but nonces have expired. 3497 $response['nonces_expired'] = true; 3498 wp_send_json( $response ); 3499 } 3500 } 3501 3502 if ( ! empty( $data ) ) { 3503 /** 3504 * Filters the Heartbeat response received. 3505 * 3506 * @since 3.6.0 3507 * 3508 * @param array $response The Heartbeat response. 3509 * @param array $data The $_POST data sent. 3510 * @param string $screen_id The screen ID. 3511 */ 3512 $response = apply_filters( 'heartbeat_received', $response, $data, $screen_id ); 3513 } 3514 3515 /** 3516 * Filters the Heartbeat response sent. 3517 * 3518 * @since 3.6.0 3519 * 3520 * @param array $response The Heartbeat response. 3521 * @param string $screen_id The screen ID. 3522 */ 3523 $response = apply_filters( 'heartbeat_send', $response, $screen_id ); 3524 3525 /** 3526 * Fires when Heartbeat ticks in logged-in environments. 3527 * 3528 * Allows the transport to be easily replaced with long-polling. 3529 * 3530 * @since 3.6.0 3531 * 3532 * @param array $response The Heartbeat response. 3533 * @param string $screen_id The screen ID. 3534 */ 3535 do_action( 'heartbeat_tick', $response, $screen_id ); 3536 3537 // Send the current time according to the server. 3538 $response['server_time'] = time(); 3539 3540 wp_send_json( $response ); 3541 } 3542 3543 /** 3544 * Handles getting revision diffs via AJAX. 3545 * 3546 * @since 3.6.0 3547 */ 3548 function wp_ajax_get_revision_diffs() { 3549 require ABSPATH . 'wp-admin/includes/revision.php'; 3550 3551 $post = get_post( (int) $_REQUEST['post_id'] ); 3552 if ( ! $post ) { 3553 wp_send_json_error(); 3554 } 3555 3556 if ( ! current_user_can( 'edit_post', $post->ID ) ) { 3557 wp_send_json_error(); 3558 } 3559 3560 // Really just pre-loading the cache here. 3561 $revisions = wp_get_post_revisions( $post->ID, array( 'check_enabled' => false ) ); 3562 if ( ! $revisions ) { 3563 wp_send_json_error(); 3564 } 3565 3566 $return = array(); 3567 3568 // Increase the script timeout limit to allow ample time for diff UI setup. 3569 if ( function_exists( 'set_time_limit' ) ) { 3570 set_time_limit( 5 * MINUTE_IN_SECONDS ); 3571 } 3572 3573 foreach ( $_REQUEST['compare'] as $compare_key ) { 3574 list( $compare_from, $compare_to ) = explode( ':', $compare_key ); // from:to 3575 3576 $return[] = array( 3577 'id' => $compare_key, 3578 'fields' => wp_get_revision_ui_diff( $post, $compare_from, $compare_to ), 3579 ); 3580 } 3581 wp_send_json_success( $return ); 3582 } 3583 3584 /** 3585 * Handles auto-saving the selected color scheme for 3586 * a user's own profile via AJAX. 3587 * 3588 * @since 3.8.0 3589 * 3590 * @global array $_wp_admin_css_colors Registered admin CSS color schemes. 3591 */ 3592 function wp_ajax_save_user_color_scheme() { 3593 global $_wp_admin_css_colors; 3594 3595 check_ajax_referer( 'save-color-scheme', 'nonce' ); 3596 3597 $color_scheme = sanitize_key( $_POST['color_scheme'] ); 3598 3599 if ( ! isset( $_wp_admin_css_colors[ $color_scheme ] ) ) { 3600 wp_send_json_error(); 3601 } 3602 3603 $previous_color_scheme = get_user_meta( get_current_user_id(), 'admin_color', true ); 3604 update_user_meta( get_current_user_id(), 'admin_color', $color_scheme ); 3605 3606 wp_send_json_success( 3607 array( 3608 'previousScheme' => 'admin-color-' . $previous_color_scheme, 3609 'currentScheme' => 'admin-color-' . $color_scheme, 3610 ) 3611 ); 3612 } 3613 3614 /** 3615 * Handles getting themes from themes_api() via AJAX. 3616 * 3617 * @since 3.9.0 3618 * 3619 * @global array $themes_allowedtags Allowed HTML tags for theme descriptions. 3620 * @global array $theme_field_defaults Default theme fields. 3621 */ 3622 function wp_ajax_query_themes() { 3623 global $themes_allowedtags, $theme_field_defaults; 3624 3625 if ( ! current_user_can( 'install_themes' ) ) { 3626 wp_send_json_error(); 3627 } 3628 3629 $args = wp_parse_args( 3630 wp_unslash( $_REQUEST['request'] ), 3631 array( 3632 'per_page' => 20, 3633 'fields' => array_merge( 3634 (array) $theme_field_defaults, 3635 array( 3636 'reviews_url' => true, // Explicitly request the reviews URL to be linked from the Add Themes screen. 3637 ) 3638 ), 3639 ) 3640 ); 3641 3642 if ( isset( $args['browse'] ) && 'favorites' === $args['browse'] && ! isset( $args['user'] ) ) { 3643 $user = get_user_option( 'wporg_favorites' ); 3644 if ( $user ) { 3645 $args['user'] = $user; 3646 } 3647 } 3648 3649 $old_filter = $args['browse'] ?? 'search'; 3650 3651 /** This filter is documented in wp-admin/includes/class-wp-theme-install-list-table.php */ 3652 $args = apply_filters( 'install_themes_table_api_args_' . $old_filter, $args ); 3653 3654 $api = themes_api( 'query_themes', $args ); 3655 3656 if ( is_wp_error( $api ) ) { 3657 wp_send_json_error(); 3658 } 3659 3660 $update_php = network_admin_url( 'update.php?action=install-theme' ); 3661 3662 $installed_themes = search_theme_directories(); 3663 3664 if ( false === $installed_themes ) { 3665 $installed_themes = array(); 3666 } 3667 3668 foreach ( $installed_themes as $theme_slug => $theme_data ) { 3669 // Ignore child themes. 3670 if ( str_contains( $theme_slug, '/' ) ) { 3671 unset( $installed_themes[ $theme_slug ] ); 3672 } 3673 } 3674 3675 foreach ( $api->themes as &$theme ) { 3676 $theme->install_url = add_query_arg( 3677 array( 3678 'theme' => $theme->slug, 3679 '_wpnonce' => wp_create_nonce( 'install-theme_' . $theme->slug ), 3680 ), 3681 $update_php 3682 ); 3683 3684 if ( current_user_can( 'switch_themes' ) ) { 3685 if ( is_multisite() ) { 3686 $theme->activate_url = add_query_arg( 3687 array( 3688 'action' => 'enable', 3689 '_wpnonce' => wp_create_nonce( 'enable-theme_' . $theme->slug ), 3690 'theme' => $theme->slug, 3691 ), 3692 network_admin_url( 'themes.php' ) 3693 ); 3694 } else { 3695 $theme->activate_url = add_query_arg( 3696 array( 3697 'action' => 'activate', 3698 '_wpnonce' => wp_create_nonce( 'switch-theme_' . $theme->slug ), 3699 'stylesheet' => $theme->slug, 3700 ), 3701 admin_url( 'themes.php' ) 3702 ); 3703 } 3704 } 3705 3706 $is_theme_installed = array_key_exists( $theme->slug, $installed_themes ); 3707 3708 // We only care about installed themes. 3709 $theme->block_theme = $is_theme_installed && wp_get_theme( $theme->slug )->is_block_theme(); 3710 3711 if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { 3712 $customize_url = $theme->block_theme ? admin_url( 'site-editor.php' ) : wp_customize_url( $theme->slug ); 3713 3714 $theme->customize_url = add_query_arg( 3715 array( 3716 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ), 3717 ), 3718 $customize_url 3719 ); 3720 } 3721 3722 $theme->name = wp_kses( $theme->name, $themes_allowedtags ); 3723 $theme->author = wp_kses( $theme->author['display_name'], $themes_allowedtags ); 3724 $theme->version = wp_kses( $theme->version, $themes_allowedtags ); 3725 $theme->description = wp_kses( $theme->description, $themes_allowedtags ); 3726 3727 $theme->stars = wp_star_rating( 3728 array( 3729 'rating' => $theme->rating, 3730 'type' => 'percent', 3731 'number' => $theme->num_ratings, 3732 'echo' => false, 3733 ) 3734 ); 3735 3736 $theme->num_ratings = number_format_i18n( $theme->num_ratings ); 3737 $theme->preview_url = set_url_scheme( $theme->preview_url ); 3738 $theme->compatible_wp = is_wp_version_compatible( $theme->requires ); 3739 $theme->compatible_php = is_php_version_compatible( $theme->requires_php ); 3740 } 3741 3742 wp_send_json_success( $api ); 3743 } 3744 3745 /** 3746 * Applies [embed] Ajax handlers to a string. 3747 * 3748 * @since 4.0.0 3749 * 3750 * @global WP_Post $post Global post object. 3751 * @global WP_Embed $wp_embed WordPress Embed object. 3752 * @global WP_Scripts $wp_scripts Script dependencies object. 3753 * @global int $content_width Shared post content width. 3754 */ 3755 function wp_ajax_parse_embed() { 3756 global $post, $wp_embed, $content_width; 3757 3758 if ( empty( $_POST['shortcode'] ) ) { 3759 wp_send_json_error(); 3760 } 3761 3762 $post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0; 3763 3764 if ( $post_id > 0 ) { 3765 $post = get_post( $post_id ); 3766 3767 if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) { 3768 wp_send_json_error(); 3769 } 3770 setup_postdata( $post ); 3771 } elseif ( ! current_user_can( 'edit_posts' ) ) { // See WP_oEmbed_Controller::get_proxy_item_permissions_check(). 3772 wp_send_json_error(); 3773 } 3774 3775 $shortcode = wp_unslash( $_POST['shortcode'] ); 3776 3777 preg_match( '/' . get_shortcode_regex() . '/s', $shortcode, $matches ); 3778 $atts = shortcode_parse_atts( $matches[3] ); 3779 3780 if ( ! empty( $matches[5] ) ) { 3781 $url = $matches[5]; 3782 } elseif ( ! empty( $atts['src'] ) ) { 3783 $url = $atts['src']; 3784 } else { 3785 $url = ''; 3786 } 3787 3788 $parsed = false; 3789 $wp_embed->return_false_on_fail = true; 3790 3791 if ( 0 === $post_id ) { 3792 /* 3793 * Refresh oEmbeds cached outside of posts that are past their TTL. 3794 * Posts are excluded because they have separate logic for refreshing 3795 * their post meta caches. See WP_Embed::cache_oembed(). 3796 */ 3797 $wp_embed->usecache = false; 3798 } 3799 3800 if ( is_ssl() && str_starts_with( $url, 'http://' ) ) { 3801 /* 3802 * Admin is ssl and the user pasted non-ssl URL. 3803 * Check if the provider supports ssl embeds and use that for the preview. 3804 */ 3805 $ssl_shortcode = preg_replace( '%^(\\[embed[^\\]]*\\])http://%i', '$1https://', $shortcode ); 3806 $parsed = $wp_embed->run_shortcode( $ssl_shortcode ); 3807 3808 if ( ! $parsed ) { 3809 $no_ssl_support = true; 3810 } 3811 } 3812 3813 // Set $content_width so any embeds fit in the destination iframe. 3814 if ( isset( $_POST['maxwidth'] ) && is_numeric( $_POST['maxwidth'] ) && $_POST['maxwidth'] > 0 ) { 3815 if ( ! isset( $content_width ) ) { 3816 $content_width = (int) $_POST['maxwidth']; 3817 } else { 3818 $content_width = min( $content_width, (int) $_POST['maxwidth'] ); 3819 } 3820 } 3821 3822 if ( $url && ! $parsed ) { 3823 $parsed = $wp_embed->run_shortcode( $shortcode ); 3824 } 3825 3826 if ( ! $parsed ) { 3827 wp_send_json_error( 3828 array( 3829 'type' => 'not-embeddable', 3830 /* translators: %s: URL that could not be embedded. */ 3831 'message' => sprintf( __( '%s failed to embed.' ), '<code>' . esc_html( $url ) . '</code>' ), 3832 ) 3833 ); 3834 } 3835 3836 if ( has_shortcode( $parsed, 'audio' ) || has_shortcode( $parsed, 'video' ) ) { 3837 $styles = ''; 3838 $mce_styles = wpview_media_sandbox_styles(); 3839 3840 foreach ( $mce_styles as $style ) { 3841 $styles .= sprintf( '<link rel="stylesheet" href="%s" />', $style ); 3842 } 3843 3844 $html = do_shortcode( $parsed ); 3845 3846 global $wp_scripts; 3847 3848 if ( ! empty( $wp_scripts ) ) { 3849 $wp_scripts->done = array(); 3850 } 3851 3852 ob_start(); 3853 wp_print_scripts( array( 'mediaelement-vimeo', 'wp-mediaelement' ) ); 3854 $scripts = ob_get_clean(); 3855 3856 $parsed = $styles . $html . $scripts; 3857 } 3858 3859 if ( ! empty( $no_ssl_support ) || ( is_ssl() && ( preg_match( '%<(iframe|script|embed) [^>]*src="http://%', $parsed ) || 3860 preg_match( '%<link [^>]*href="http://%', $parsed ) ) ) ) { 3861 // Admin is ssl and the embed is not. Iframes, scripts, and other "active content" will be blocked. 3862 wp_send_json_error( 3863 array( 3864 'type' => 'not-ssl', 3865 'message' => __( 'This preview is unavailable in the editor.' ), 3866 ) 3867 ); 3868 } 3869 3870 $return = array( 3871 'body' => $parsed, 3872 'attr' => $wp_embed->last_attr, 3873 ); 3874 3875 if ( str_contains( $parsed, 'class="wp-embedded-content' ) ) { 3876 if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) { 3877 $script_src = includes_url( 'js/wp-embed.js' ); 3878 } else { 3879 $script_src = includes_url( 'js/wp-embed.min.js' ); 3880 } 3881 3882 $return['head'] = '<script src="' . $script_src . '"></script>'; 3883 $return['sandbox'] = true; 3884 } 3885 3886 wp_send_json_success( $return ); 3887 } 3888 3889 /** 3890 * @since 4.0.0 3891 * 3892 * @global WP_Post $post Global post object. 3893 * @global WP_Scripts $wp_scripts Script dependencies object. 3894 */ 3895 function wp_ajax_parse_media_shortcode() { 3896 global $post, $wp_scripts; 3897 3898 if ( empty( $_POST['shortcode'] ) ) { 3899 wp_send_json_error(); 3900 } 3901 3902 $shortcode = wp_unslash( $_POST['shortcode'] ); 3903 3904 // Only process previews for media related shortcodes: 3905 $found_shortcodes = get_shortcode_tags_in_content( $shortcode ); 3906 $media_shortcodes = array( 3907 'audio', 3908 'embed', 3909 'playlist', 3910 'video', 3911 'gallery', 3912 ); 3913 3914 $other_shortcodes = array_diff( $found_shortcodes, $media_shortcodes ); 3915 3916 if ( ! empty( $other_shortcodes ) ) { 3917 wp_send_json_error(); 3918 } 3919 3920 if ( ! empty( $_POST['post_ID'] ) ) { 3921 $post = get_post( (int) $_POST['post_ID'] ); 3922 } 3923 3924 // The embed shortcode requires a post. 3925 if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) { 3926 if ( in_array( 'embed', $found_shortcodes, true ) ) { 3927 wp_send_json_error(); 3928 } 3929 } else { 3930 setup_postdata( $post ); 3931 } 3932 3933 $parsed = do_shortcode( $shortcode ); 3934 3935 if ( empty( $parsed ) ) { 3936 wp_send_json_error( 3937 array( 3938 'type' => 'no-items', 3939 'message' => __( 'No items found.' ), 3940 ) 3941 ); 3942 } 3943 3944 $head = ''; 3945 $styles = wpview_media_sandbox_styles(); 3946 3947 foreach ( $styles as $style ) { 3948 $head .= '<link rel="stylesheet" href="' . $style . '">'; 3949 } 3950 3951 if ( ! empty( $wp_scripts ) ) { 3952 $wp_scripts->done = array(); 3953 } 3954 3955 ob_start(); 3956 3957 echo $parsed; 3958 3959 if ( 'playlist' === $_REQUEST['type'] ) { 3960 wp_underscore_playlist_templates(); 3961 3962 wp_print_scripts( 'wp-playlist' ); 3963 } else { 3964 wp_print_scripts( array( 'mediaelement-vimeo', 'wp-mediaelement' ) ); 3965 } 3966 3967 wp_send_json_success( 3968 array( 3969 'head' => $head, 3970 'body' => ob_get_clean(), 3971 ) 3972 ); 3973 } 3974 3975 /** 3976 * Handles destroying multiple open sessions for a user via AJAX. 3977 * 3978 * @since 4.1.0 3979 */ 3980 function wp_ajax_destroy_sessions() { 3981 $user = get_userdata( (int) $_POST['user_id'] ); 3982 3983 if ( $user ) { 3984 if ( ! current_user_can( 'edit_user', $user->ID ) ) { 3985 $user = false; 3986 } elseif ( ! wp_verify_nonce( $_POST['nonce'], 'update-user_' . $user->ID ) ) { 3987 $user = false; 3988 } 3989 } 3990 3991 if ( ! $user ) { 3992 wp_send_json_error( 3993 array( 3994 'message' => __( 'Could not log out user sessions. Please try again.' ), 3995 ) 3996 ); 3997 } 3998 3999 $sessions = WP_Session_Tokens::get_instance( $user->ID ); 4000 4001 if ( get_current_user_id() === $user->ID ) { 4002 $sessions->destroy_others( wp_get_session_token() ); 4003 $message = __( 'You are now logged out everywhere else.' ); 4004 } else { 4005 $sessions->destroy_all(); 4006 /* translators: %s: User's display name. */ 4007 $message = sprintf( __( '%s has been logged out.' ), $user->display_name ); 4008 } 4009 4010 wp_send_json_success( array( 'message' => $message ) ); 4011 } 4012 4013 /** 4014 * Handles cropping an image via AJAX. 4015 * 4016 * @since 4.3.0 4017 */ 4018 function wp_ajax_crop_image() { 4019 $attachment_id = absint( $_POST['id'] ); 4020 4021 check_ajax_referer( 'image_editor-' . $attachment_id, 'nonce' ); 4022 4023 if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) { 4024 wp_send_json_error(); 4025 } 4026 4027 $context = str_replace( '_', '-', $_POST['context'] ); 4028 $data = array_map( 'absint', $_POST['cropDetails'] ); 4029 $cropped = wp_crop_image( $attachment_id, $data['x1'], $data['y1'], $data['width'], $data['height'], $data['dst_width'], $data['dst_height'] ); 4030 4031 if ( ! $cropped || is_wp_error( $cropped ) ) { 4032 wp_send_json_error( array( 'message' => __( 'Image could not be processed.' ) ) ); 4033 } 4034 4035 switch ( $context ) { 4036 case 'site-icon': 4037 require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php'; 4038 $wp_site_icon = new WP_Site_Icon(); 4039 4040 // Skip creating a new attachment if the attachment is a Site Icon. 4041 if ( get_post_meta( $attachment_id, '_wp_attachment_context', true ) === $context ) { 4042 4043 // Delete the temporary cropped file, we don't need it. 4044 wp_delete_file( $cropped ); 4045 4046 // Additional sizes in wp_prepare_attachment_for_js(). 4047 add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) ); 4048 break; 4049 } 4050 4051 /** This filter is documented in wp-admin/includes/class-custom-image-header.php */ 4052 $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. 4053 4054 // Copy attachment properties. 4055 $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context ); 4056 4057 // Update the attachment. 4058 add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) ); 4059 $attachment_id = $wp_site_icon->insert_attachment( $attachment, $cropped ); 4060 remove_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) ); 4061 4062 // Additional sizes in wp_prepare_attachment_for_js(). 4063 add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) ); 4064 break; 4065 4066 default: 4067 /** 4068 * Fires before a cropped image is saved. 4069 * 4070 * Allows to add filters to modify the way a cropped image is saved. 4071 * 4072 * @since 4.3.0 4073 * 4074 * @param string $context The Customizer control requesting the cropped image. 4075 * @param int $attachment_id The attachment ID of the original image. 4076 * @param string $cropped Path to the cropped image file. 4077 */ 4078 do_action( 'wp_ajax_crop_image_pre_save', $context, $attachment_id, $cropped ); 4079 4080 /** This filter is documented in wp-admin/includes/class-custom-image-header.php */ 4081 $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. 4082 4083 // Copy attachment properties. 4084 $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context ); 4085 4086 $attachment_id = wp_insert_attachment( $attachment, $cropped ); 4087 $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); 4088 4089 /** 4090 * Filters the cropped image attachment metadata. 4091 * 4092 * @since 4.3.0 4093 * 4094 * @see wp_generate_attachment_metadata() 4095 * 4096 * @param array $metadata Attachment metadata. 4097 */ 4098 $metadata = apply_filters( 'wp_ajax_cropped_attachment_metadata', $metadata ); 4099 wp_update_attachment_metadata( $attachment_id, $metadata ); 4100 4101 /** 4102 * Filters the attachment ID for a cropped image. 4103 * 4104 * @since 4.3.0 4105 * 4106 * @param int $attachment_id The attachment ID of the cropped image. 4107 * @param string $context The Customizer control requesting the cropped image. 4108 */ 4109 $attachment_id = apply_filters( 'wp_ajax_cropped_attachment_id', $attachment_id, $context ); 4110 } 4111 4112 wp_send_json_success( wp_prepare_attachment_for_js( $attachment_id ) ); 4113 } 4114 4115 /** 4116 * Handles generating a password via AJAX. 4117 * 4118 * @since 4.4.0 4119 */ 4120 function wp_ajax_generate_password() { 4121 wp_send_json_success( wp_generate_password( 24 ) ); 4122 } 4123 4124 /** 4125 * Handles generating a password in the no-privilege context via AJAX. 4126 * 4127 * @since 5.7.0 4128 */ 4129 function wp_ajax_nopriv_generate_password() { 4130 wp_send_json_success( wp_generate_password( 24 ) ); 4131 } 4132 4133 /** 4134 * Handles saving the user's WordPress.org username via AJAX. 4135 * 4136 * @since 4.4.0 4137 */ 4138 function wp_ajax_save_wporg_username() { 4139 if ( ! current_user_can( 'install_themes' ) && ! current_user_can( 'install_plugins' ) ) { 4140 wp_send_json_error(); 4141 } 4142 4143 check_ajax_referer( 'save_wporg_username_' . get_current_user_id() ); 4144 4145 $username = isset( $_REQUEST['username'] ) ? wp_unslash( $_REQUEST['username'] ) : false; 4146 4147 if ( ! $username ) { 4148 wp_send_json_error(); 4149 } 4150 4151 wp_send_json_success( update_user_meta( get_current_user_id(), 'wporg_favorites', $username ) ); 4152 } 4153 4154 /** 4155 * Handles installing a theme via AJAX. 4156 * 4157 * @since 4.6.0 4158 * 4159 * @see Theme_Upgrader 4160 * 4161 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 4162 */ 4163 function wp_ajax_install_theme() { 4164 check_ajax_referer( 'updates' ); 4165 4166 if ( empty( $_POST['slug'] ) ) { 4167 wp_send_json_error( 4168 array( 4169 'slug' => '', 4170 'errorCode' => 'no_theme_specified', 4171 'errorMessage' => __( 'No theme specified.' ), 4172 ) 4173 ); 4174 } 4175 4176 $slug = sanitize_key( wp_unslash( $_POST['slug'] ) ); 4177 4178 $status = array( 4179 'install' => 'theme', 4180 'slug' => $slug, 4181 ); 4182 4183 if ( ! current_user_can( 'install_themes' ) ) { 4184 $status['errorMessage'] = __( 'Sorry, you are not allowed to install themes on this site.' ); 4185 wp_send_json_error( $status ); 4186 } 4187 4188 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 4189 require_once ABSPATH . 'wp-admin/includes/theme.php'; 4190 4191 $api = themes_api( 4192 'theme_information', 4193 array( 4194 'slug' => $slug, 4195 'fields' => array( 'sections' => false ), 4196 ) 4197 ); 4198 4199 if ( is_wp_error( $api ) ) { 4200 $status['errorMessage'] = $api->get_error_message(); 4201 wp_send_json_error( $status ); 4202 } 4203 4204 $skin = new WP_Ajax_Upgrader_Skin(); 4205 $upgrader = new Theme_Upgrader( $skin ); 4206 $result = $upgrader->install( $api->download_link ); 4207 4208 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 4209 $status['debug'] = $skin->get_upgrade_messages(); 4210 } 4211 4212 if ( is_wp_error( $result ) ) { 4213 $status['errorCode'] = $result->get_error_code(); 4214 $status['errorMessage'] = $result->get_error_message(); 4215 wp_send_json_error( $status ); 4216 } elseif ( is_wp_error( $skin->result ) ) { 4217 $status['errorCode'] = $skin->result->get_error_code(); 4218 $status['errorMessage'] = $skin->result->get_error_message(); 4219 wp_send_json_error( $status ); 4220 } elseif ( $skin->get_errors()->has_errors() ) { 4221 $status['errorMessage'] = $skin->get_error_messages(); 4222 wp_send_json_error( $status ); 4223 } elseif ( is_null( $result ) ) { 4224 global $wp_filesystem; 4225 4226 $status['errorCode'] = 'unable_to_connect_to_filesystem'; 4227 $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 4228 4229 // Pass through the error from WP_Filesystem if one was raised. 4230 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { 4231 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 4232 } 4233 4234 wp_send_json_error( $status ); 4235 } 4236 4237 $status['themeName'] = wp_get_theme( $slug )->get( 'Name' ); 4238 4239 if ( current_user_can( 'switch_themes' ) ) { 4240 if ( is_multisite() ) { 4241 $status['activateUrl'] = add_query_arg( 4242 array( 4243 'action' => 'enable', 4244 '_wpnonce' => wp_create_nonce( 'enable-theme_' . $slug ), 4245 'theme' => $slug, 4246 ), 4247 network_admin_url( 'themes.php' ) 4248 ); 4249 } else { 4250 $status['activateUrl'] = add_query_arg( 4251 array( 4252 'action' => 'activate', 4253 '_wpnonce' => wp_create_nonce( 'switch-theme_' . $slug ), 4254 'stylesheet' => $slug, 4255 ), 4256 admin_url( 'themes.php' ) 4257 ); 4258 } 4259 } 4260 4261 $theme = wp_get_theme( $slug ); 4262 $status['blockTheme'] = $theme->is_block_theme(); 4263 4264 if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { 4265 $status['customizeUrl'] = add_query_arg( 4266 array( 4267 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ), 4268 ), 4269 wp_customize_url( $slug ) 4270 ); 4271 } 4272 4273 /* 4274 * See WP_Theme_Install_List_Table::_get_theme_status() if we wanted to check 4275 * on post-installation status. 4276 */ 4277 wp_send_json_success( $status ); 4278 } 4279 4280 /** 4281 * Handles updating a theme via AJAX. 4282 * 4283 * @since 4.6.0 4284 * 4285 * @see Theme_Upgrader 4286 * 4287 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 4288 */ 4289 function wp_ajax_update_theme() { 4290 check_ajax_referer( 'updates' ); 4291 4292 if ( empty( $_POST['slug'] ) ) { 4293 wp_send_json_error( 4294 array( 4295 'slug' => '', 4296 'errorCode' => 'no_theme_specified', 4297 'errorMessage' => __( 'No theme specified.' ), 4298 ) 4299 ); 4300 } 4301 4302 $stylesheet = preg_replace( '/[^A-z0-9_\-]/', '', wp_unslash( $_POST['slug'] ) ); 4303 $status = array( 4304 'update' => 'theme', 4305 'slug' => $stylesheet, 4306 'oldVersion' => '', 4307 'newVersion' => '', 4308 ); 4309 4310 if ( ! current_user_can( 'update_themes' ) ) { 4311 $status['errorMessage'] = __( 'Sorry, you are not allowed to update themes for this site.' ); 4312 wp_send_json_error( $status ); 4313 } 4314 4315 $theme = wp_get_theme( $stylesheet ); 4316 if ( $theme->exists() ) { 4317 $status['oldVersion'] = $theme->get( 'Version' ); 4318 } 4319 4320 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 4321 4322 $current = get_site_transient( 'update_themes' ); 4323 if ( empty( $current ) ) { 4324 wp_update_themes(); 4325 } 4326 4327 $skin = new WP_Ajax_Upgrader_Skin(); 4328 $upgrader = new Theme_Upgrader( $skin ); 4329 $result = $upgrader->bulk_upgrade( array( $stylesheet ) ); 4330 4331 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 4332 $status['debug'] = $skin->get_upgrade_messages(); 4333 } 4334 4335 if ( is_wp_error( $skin->result ) ) { 4336 $status['errorCode'] = $skin->result->get_error_code(); 4337 $status['errorMessage'] = $skin->result->get_error_message(); 4338 wp_send_json_error( $status ); 4339 } elseif ( $skin->get_errors()->has_errors() ) { 4340 $status['errorMessage'] = $skin->get_error_messages(); 4341 wp_send_json_error( $status ); 4342 } elseif ( is_array( $result ) && ! empty( $result[ $stylesheet ] ) ) { 4343 4344 // Theme is already at the latest version. 4345 if ( true === $result[ $stylesheet ] ) { 4346 $status['errorMessage'] = $upgrader->strings['up_to_date']; 4347 wp_send_json_error( $status ); 4348 } 4349 4350 $theme = wp_get_theme( $stylesheet ); 4351 if ( $theme->exists() ) { 4352 $status['newVersion'] = $theme->get( 'Version' ); 4353 } 4354 4355 wp_send_json_success( $status ); 4356 } elseif ( false === $result ) { 4357 global $wp_filesystem; 4358 4359 $status['errorCode'] = 'unable_to_connect_to_filesystem'; 4360 $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 4361 4362 // Pass through the error from WP_Filesystem if one was raised. 4363 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { 4364 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 4365 } 4366 4367 wp_send_json_error( $status ); 4368 } 4369 4370 // An unhandled error occurred. 4371 $status['errorMessage'] = __( 'Theme update failed.' ); 4372 wp_send_json_error( $status ); 4373 } 4374 4375 /** 4376 * Handles deleting a theme via AJAX. 4377 * 4378 * @since 4.6.0 4379 * 4380 * @see delete_theme() 4381 * 4382 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 4383 */ 4384 function wp_ajax_delete_theme() { 4385 check_ajax_referer( 'updates' ); 4386 4387 if ( empty( $_POST['slug'] ) ) { 4388 wp_send_json_error( 4389 array( 4390 'slug' => '', 4391 'errorCode' => 'no_theme_specified', 4392 'errorMessage' => __( 'No theme specified.' ), 4393 ) 4394 ); 4395 } 4396 4397 $stylesheet = preg_replace( '/[^A-z0-9_\-]/', '', wp_unslash( $_POST['slug'] ) ); 4398 $status = array( 4399 'delete' => 'theme', 4400 'slug' => $stylesheet, 4401 ); 4402 4403 if ( ! current_user_can( 'delete_themes' ) ) { 4404 $status['errorMessage'] = __( 'Sorry, you are not allowed to delete themes on this site.' ); 4405 wp_send_json_error( $status ); 4406 } 4407 4408 if ( ! wp_get_theme( $stylesheet )->exists() ) { 4409 $status['errorMessage'] = __( 'The requested theme does not exist.' ); 4410 wp_send_json_error( $status ); 4411 } 4412 4413 // Check filesystem credentials. `delete_theme()` will bail otherwise. 4414 $url = wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ); 4415 4416 ob_start(); 4417 $credentials = request_filesystem_credentials( $url ); 4418 ob_end_clean(); 4419 4420 if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { 4421 global $wp_filesystem; 4422 4423 $status['errorCode'] = 'unable_to_connect_to_filesystem'; 4424 $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 4425 4426 // Pass through the error from WP_Filesystem if one was raised. 4427 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { 4428 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 4429 } 4430 4431 wp_send_json_error( $status ); 4432 } 4433 4434 require_once ABSPATH . 'wp-admin/includes/theme.php'; 4435 4436 $result = delete_theme( $stylesheet ); 4437 4438 if ( is_wp_error( $result ) ) { 4439 $status['errorMessage'] = $result->get_error_message(); 4440 wp_send_json_error( $status ); 4441 } elseif ( false === $result ) { 4442 $status['errorMessage'] = __( 'Theme could not be deleted.' ); 4443 wp_send_json_error( $status ); 4444 } 4445 4446 wp_send_json_success( $status ); 4447 } 4448 4449 /** 4450 * Handles installing a plugin via AJAX. 4451 * 4452 * @since 4.6.0 4453 * 4454 * @see Plugin_Upgrader 4455 * 4456 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 4457 */ 4458 function wp_ajax_install_plugin() { 4459 check_ajax_referer( 'updates' ); 4460 4461 if ( empty( $_POST['slug'] ) ) { 4462 wp_send_json_error( 4463 array( 4464 'slug' => '', 4465 'errorCode' => 'no_plugin_specified', 4466 'errorMessage' => __( 'No plugin specified.' ), 4467 ) 4468 ); 4469 } 4470 4471 $status = array( 4472 'install' => 'plugin', 4473 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ), 4474 ); 4475 4476 if ( ! current_user_can( 'install_plugins' ) ) { 4477 $status['errorMessage'] = __( 'Sorry, you are not allowed to install plugins on this site.' ); 4478 wp_send_json_error( $status ); 4479 } 4480 4481 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 4482 require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; 4483 4484 $api = plugins_api( 4485 'plugin_information', 4486 array( 4487 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ), 4488 'fields' => array( 4489 'sections' => false, 4490 ), 4491 ) 4492 ); 4493 4494 if ( is_wp_error( $api ) ) { 4495 $status['errorMessage'] = $api->get_error_message(); 4496 wp_send_json_error( $status ); 4497 } 4498 4499 $status['pluginName'] = $api->name; 4500 4501 $skin = new WP_Ajax_Upgrader_Skin(); 4502 $upgrader = new Plugin_Upgrader( $skin ); 4503 $result = $upgrader->install( $api->download_link ); 4504 4505 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 4506 $status['debug'] = $skin->get_upgrade_messages(); 4507 } 4508 4509 if ( is_wp_error( $result ) ) { 4510 $status['errorCode'] = $result->get_error_code(); 4511 $status['errorMessage'] = $result->get_error_message(); 4512 wp_send_json_error( $status ); 4513 } elseif ( is_wp_error( $skin->result ) ) { 4514 $status['errorCode'] = $skin->result->get_error_code(); 4515 $status['errorMessage'] = $skin->result->get_error_message(); 4516 wp_send_json_error( $status ); 4517 } elseif ( $skin->get_errors()->has_errors() ) { 4518 $status['errorMessage'] = $skin->get_error_messages(); 4519 wp_send_json_error( $status ); 4520 } elseif ( is_null( $result ) ) { 4521 global $wp_filesystem; 4522 4523 $status['errorCode'] = 'unable_to_connect_to_filesystem'; 4524 $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 4525 4526 // Pass through the error from WP_Filesystem if one was raised. 4527 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { 4528 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 4529 } 4530 4531 wp_send_json_error( $status ); 4532 } 4533 4534 $install_status = install_plugin_install_status( $api ); 4535 $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : ''; 4536 4537 // If installation request is coming from import page, do not return network activation link. 4538 $plugins_url = ( 'import' === $pagenow ) ? admin_url( 'plugins.php' ) : network_admin_url( 'plugins.php' ); 4539 4540 if ( current_user_can( 'activate_plugin', $install_status['file'] ) && is_plugin_inactive( $install_status['file'] ) ) { 4541 $status['activateUrl'] = add_query_arg( 4542 array( 4543 '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $install_status['file'] ), 4544 'action' => 'activate', 4545 'plugin' => $install_status['file'], 4546 ), 4547 $plugins_url 4548 ); 4549 } 4550 4551 if ( is_multisite() && current_user_can( 'manage_network_plugins' ) && 'import' !== $pagenow ) { 4552 $status['activateUrl'] = add_query_arg( array( 'networkwide' => 1 ), $status['activateUrl'] ); 4553 } 4554 4555 wp_send_json_success( $status ); 4556 } 4557 4558 /** 4559 * Handles activating a plugin via AJAX. 4560 * 4561 * @since 6.5.0 4562 */ 4563 function wp_ajax_activate_plugin() { 4564 check_ajax_referer( 'updates' ); 4565 4566 if ( empty( $_POST['name'] ) || empty( $_POST['slug'] ) || empty( $_POST['plugin'] ) ) { 4567 wp_send_json_error( 4568 array( 4569 'slug' => '', 4570 'pluginName' => '', 4571 'plugin' => '', 4572 'errorCode' => 'no_plugin_specified', 4573 'errorMessage' => __( 'No plugin specified.' ), 4574 ) 4575 ); 4576 } 4577 4578 $status = array( 4579 'activate' => 'plugin', 4580 'slug' => wp_unslash( $_POST['slug'] ), 4581 'pluginName' => wp_unslash( $_POST['name'] ), 4582 'plugin' => wp_unslash( $_POST['plugin'] ), 4583 ); 4584 4585 if ( ! current_user_can( 'activate_plugin', $status['plugin'] ) ) { 4586 $status['errorMessage'] = __( 'Sorry, you are not allowed to activate plugins on this site.' ); 4587 wp_send_json_error( $status ); 4588 } 4589 4590 // A network-only plugin is activated for the entire network. 4591 if ( is_multisite() && is_network_only_plugin( $status['plugin'] ) && ! current_user_can( 'manage_network_plugins' ) ) { 4592 $status['errorMessage'] = __( 'Sorry, you are not allowed to activate this plugin.' ); 4593 wp_send_json_error( $status ); 4594 } 4595 4596 if ( is_plugin_active( $status['plugin'] ) ) { 4597 $status['errorMessage'] = sprintf( 4598 /* translators: %s: Plugin name. */ 4599 __( '%s is already active.' ), 4600 $status['pluginName'] 4601 ); 4602 } 4603 4604 $activated = activate_plugin( $status['plugin'] ); 4605 4606 if ( is_wp_error( $activated ) ) { 4607 $status['errorMessage'] = $activated->get_error_message(); 4608 wp_send_json_error( $status ); 4609 } 4610 4611 wp_send_json_success( $status ); 4612 } 4613 4614 /** 4615 * Handles updating a plugin via AJAX. 4616 * 4617 * @since 4.2.0 4618 * 4619 * @see Plugin_Upgrader 4620 * 4621 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 4622 */ 4623 function wp_ajax_update_plugin() { 4624 check_ajax_referer( 'updates' ); 4625 4626 if ( empty( $_POST['plugin'] ) || empty( $_POST['slug'] ) ) { 4627 wp_send_json_error( 4628 array( 4629 'slug' => '', 4630 'errorCode' => 'no_plugin_specified', 4631 'errorMessage' => __( 'No plugin specified.' ), 4632 ) 4633 ); 4634 } 4635 4636 $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) ); 4637 4638 $status = array( 4639 'update' => 'plugin', 4640 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ), 4641 'oldVersion' => '', 4642 'newVersion' => '', 4643 ); 4644 4645 if ( ! current_user_can( 'update_plugins' ) || 0 !== validate_file( $plugin ) ) { 4646 $status['errorMessage'] = __( 'Sorry, you are not allowed to update plugins for this site.' ); 4647 wp_send_json_error( $status ); 4648 } 4649 4650 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 4651 $status['plugin'] = $plugin; 4652 $status['pluginName'] = $plugin_data['Name']; 4653 4654 if ( $plugin_data['Version'] ) { 4655 /* translators: %s: Plugin version. */ 4656 $status['oldVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] ); 4657 } 4658 4659 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 4660 4661 wp_update_plugins(); 4662 4663 $skin = new WP_Ajax_Upgrader_Skin(); 4664 $upgrader = new Plugin_Upgrader( $skin ); 4665 $result = $upgrader->bulk_upgrade( array( $plugin ) ); 4666 4667 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 4668 $status['debug'] = $skin->get_upgrade_messages(); 4669 } 4670 4671 if ( is_wp_error( $skin->result ) ) { 4672 $status['errorCode'] = $skin->result->get_error_code(); 4673 $status['errorMessage'] = $skin->result->get_error_message(); 4674 wp_send_json_error( $status ); 4675 } elseif ( $skin->get_errors()->has_errors() ) { 4676 $status['errorMessage'] = $skin->get_error_messages(); 4677 wp_send_json_error( $status ); 4678 } elseif ( is_array( $result ) && ! empty( $result[ $plugin ] ) ) { 4679 4680 /* 4681 * Plugin is already at the latest version. 4682 * 4683 * This may also be the return value if the `update_plugins` site transient is empty, 4684 * e.g. when you update two plugins in quick succession before the transient repopulates. 4685 * 4686 * Preferably something can be done to ensure `update_plugins` isn't empty. 4687 * For now, surface some sort of error here. 4688 */ 4689 if ( true === $result[ $plugin ] ) { 4690 $status['errorMessage'] = $upgrader->strings['up_to_date']; 4691 wp_send_json_error( $status ); 4692 } 4693 4694 $plugin_data = get_plugins( '/' . $result[ $plugin ]['destination_name'] ); 4695 $plugin_data = reset( $plugin_data ); 4696 4697 if ( $plugin_data['Version'] ) { 4698 /* translators: %s: Plugin version. */ 4699 $status['newVersion'] = sprintf( __( 'Version %s' ), $plugin_data['Version'] ); 4700 } 4701 4702 wp_send_json_success( $status ); 4703 } elseif ( false === $result ) { 4704 global $wp_filesystem; 4705 4706 $status['errorCode'] = 'unable_to_connect_to_filesystem'; 4707 $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 4708 4709 // Pass through the error from WP_Filesystem if one was raised. 4710 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { 4711 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 4712 } 4713 4714 wp_send_json_error( $status ); 4715 } 4716 4717 // An unhandled error occurred. 4718 $status['errorMessage'] = __( 'Plugin update failed.' ); 4719 wp_send_json_error( $status ); 4720 } 4721 4722 /** 4723 * Handles deleting a plugin via AJAX. 4724 * 4725 * @since 4.6.0 4726 * 4727 * @see delete_plugins() 4728 * 4729 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass. 4730 */ 4731 function wp_ajax_delete_plugin() { 4732 check_ajax_referer( 'updates' ); 4733 4734 if ( empty( $_POST['slug'] ) || empty( $_POST['plugin'] ) ) { 4735 wp_send_json_error( 4736 array( 4737 'slug' => '', 4738 'errorCode' => 'no_plugin_specified', 4739 'errorMessage' => __( 'No plugin specified.' ), 4740 ) 4741 ); 4742 } 4743 4744 $plugin = plugin_basename( sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) ); 4745 4746 $status = array( 4747 'delete' => 'plugin', 4748 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ), 4749 ); 4750 4751 if ( ! current_user_can( 'delete_plugins' ) || 0 !== validate_file( $plugin ) ) { 4752 $status['errorMessage'] = __( 'Sorry, you are not allowed to delete plugins for this site.' ); 4753 wp_send_json_error( $status ); 4754 } 4755 4756 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 4757 $status['plugin'] = $plugin; 4758 $status['pluginName'] = $plugin_data['Name']; 4759 4760 if ( is_plugin_active( $plugin ) ) { 4761 $status['errorMessage'] = __( 'You cannot delete a plugin while it is active on the main site.' ); 4762 wp_send_json_error( $status ); 4763 } 4764 4765 // Check filesystem credentials. `delete_plugins()` will bail otherwise. 4766 $url = wp_nonce_url( 'plugins.php?action=delete-selected&verify-delete=1&checked[]=' . $plugin, 'bulk-plugins' ); 4767 4768 ob_start(); 4769 $credentials = request_filesystem_credentials( $url ); 4770 ob_end_clean(); 4771 4772 if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { 4773 global $wp_filesystem; 4774 4775 $status['errorCode'] = 'unable_to_connect_to_filesystem'; 4776 $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 4777 4778 // Pass through the error from WP_Filesystem if one was raised. 4779 if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) { 4780 $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 4781 } 4782 4783 wp_send_json_error( $status ); 4784 } 4785 4786 $result = delete_plugins( array( $plugin ) ); 4787 4788 if ( is_wp_error( $result ) ) { 4789 $status['errorMessage'] = $result->get_error_message(); 4790 wp_send_json_error( $status ); 4791 } elseif ( false === $result ) { 4792 $status['errorMessage'] = __( 'Plugin could not be deleted.' ); 4793 wp_send_json_error( $status ); 4794 } 4795 4796 wp_send_json_success( $status ); 4797 } 4798 4799 /** 4800 * Handles searching plugins via AJAX. 4801 * 4802 * @since 4.6.0 4803 * 4804 * @global string $s Search term. 4805 */ 4806 function wp_ajax_search_plugins() { 4807 check_ajax_referer( 'updates' ); 4808 4809 // Ensure after_plugin_row_{$plugin_file} gets hooked. 4810 wp_plugin_update_rows(); 4811 4812 WP_Plugin_Dependencies::initialize(); 4813 4814 $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : ''; 4815 if ( 'plugins-network' === $pagenow || 'plugins' === $pagenow ) { 4816 set_current_screen( $pagenow ); 4817 } 4818 4819 /** @var WP_Plugins_List_Table $wp_list_table */ 4820 $wp_list_table = _get_list_table( 4821 'WP_Plugins_List_Table', 4822 array( 4823 'screen' => get_current_screen(), 4824 ) 4825 ); 4826 4827 $status = array(); 4828 4829 if ( ! $wp_list_table->ajax_user_can() ) { 4830 $status['errorMessage'] = __( 'Sorry, you are not allowed to manage plugins for this site.' ); 4831 wp_send_json_error( $status ); 4832 } 4833 4834 // Set the correct requester, so pagination works. 4835 $_SERVER['REQUEST_URI'] = add_query_arg( 4836 array_diff_key( 4837 $_POST, 4838 array( 4839 '_ajax_nonce' => null, 4840 'action' => null, 4841 ) 4842 ), 4843 network_admin_url( 'plugins.php', 'relative' ) 4844 ); 4845 4846 $GLOBALS['s'] = wp_unslash( $_POST['s'] ); 4847 4848 $wp_list_table->prepare_items(); 4849 4850 ob_start(); 4851 $wp_list_table->display(); 4852 $status['count'] = count( $wp_list_table->items ); 4853 $status['items'] = ob_get_clean(); 4854 4855 wp_send_json_success( $status ); 4856 } 4857 4858 /** 4859 * Handles searching plugins to install via AJAX. 4860 * 4861 * @since 4.6.0 4862 */ 4863 function wp_ajax_search_install_plugins() { 4864 check_ajax_referer( 'updates' ); 4865 4866 $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : ''; 4867 if ( 'plugin-install-network' === $pagenow || 'plugin-install' === $pagenow ) { 4868 set_current_screen( $pagenow ); 4869 } 4870 4871 /** @var WP_Plugin_Install_List_Table $wp_list_table */ 4872 $wp_list_table = _get_list_table( 4873 'WP_Plugin_Install_List_Table', 4874 array( 4875 'screen' => get_current_screen(), 4876 ) 4877 ); 4878 4879 $status = array(); 4880 4881 if ( ! $wp_list_table->ajax_user_can() ) { 4882 $status['errorMessage'] = __( 'Sorry, you are not allowed to manage plugins for this site.' ); 4883 wp_send_json_error( $status ); 4884 } 4885 4886 // Set the correct requester, so pagination works. 4887 $_SERVER['REQUEST_URI'] = add_query_arg( 4888 array_diff_key( 4889 $_POST, 4890 array( 4891 '_ajax_nonce' => null, 4892 'action' => null, 4893 ) 4894 ), 4895 network_admin_url( 'plugin-install.php', 'relative' ) 4896 ); 4897 4898 $wp_list_table->prepare_items(); 4899 4900 ob_start(); 4901 $wp_list_table->display(); 4902 $status['count'] = (int) $wp_list_table->get_pagination_arg( 'total_items' ); 4903 $status['items'] = ob_get_clean(); 4904 4905 wp_send_json_success( $status ); 4906 } 4907 4908 /** 4909 * Handles editing a theme or plugin file via AJAX. 4910 * 4911 * @since 4.9.0 4912 * 4913 * @see wp_edit_theme_plugin_file() 4914 */ 4915 function wp_ajax_edit_theme_plugin_file() { 4916 $edit_result = wp_edit_theme_plugin_file( wp_unslash( $_POST ) ); // Validation of args is done in wp_edit_theme_plugin_file(). 4917 4918 if ( is_wp_error( $edit_result ) ) { 4919 wp_send_json_error( 4920 array_merge( 4921 array( 4922 'code' => $edit_result->get_error_code(), 4923 'message' => $edit_result->get_error_message(), 4924 ), 4925 (array) $edit_result->get_error_data() 4926 ) 4927 ); 4928 } else { 4929 wp_send_json_success( 4930 array( 4931 'message' => __( 'File edited successfully.' ), 4932 ) 4933 ); 4934 } 4935 } 4936 4937 /** 4938 * Handles exporting a user's personal data via AJAX. 4939 * 4940 * @since 4.9.6 4941 */ 4942 function wp_ajax_wp_privacy_export_personal_data() { 4943 4944 if ( empty( $_POST['id'] ) ) { 4945 wp_send_json_error( __( 'Missing request ID.' ) ); 4946 } 4947 4948 $request_id = (int) $_POST['id']; 4949 4950 if ( $request_id < 1 ) { 4951 wp_send_json_error( __( 'Invalid request ID.' ) ); 4952 } 4953 4954 if ( ! current_user_can( 'export_others_personal_data' ) ) { 4955 wp_send_json_error( __( 'Sorry, you are not allowed to perform this action.' ) ); 4956 } 4957 4958 check_ajax_referer( 'wp-privacy-export-personal-data-' . $request_id, 'security' ); 4959 4960 // Get the request. 4961 $request = wp_get_user_request( $request_id ); 4962 4963 if ( ! $request || 'export_personal_data' !== $request->action_name ) { 4964 wp_send_json_error( __( 'Invalid request type.' ) ); 4965 } 4966 4967 $email_address = $request->email; 4968 if ( ! is_email( $email_address ) ) { 4969 wp_send_json_error( __( 'A valid email address must be given.' ) ); 4970 } 4971 4972 if ( ! isset( $_POST['exporter'] ) ) { 4973 wp_send_json_error( __( 'Missing exporter index.' ) ); 4974 } 4975 4976 $exporter_index = (int) $_POST['exporter']; 4977 4978 if ( ! isset( $_POST['page'] ) ) { 4979 wp_send_json_error( __( 'Missing page index.' ) ); 4980 } 4981 4982 $page = (int) $_POST['page']; 4983 4984 $send_as_email = isset( $_POST['sendAsEmail'] ) ? ( 'true' === $_POST['sendAsEmail'] ) : false; 4985 4986 /** 4987 * Filters the array of exporter callbacks. 4988 * 4989 * @since 4.9.6 4990 * 4991 * @param array $args { 4992 * An array of callable exporters of personal data. Default empty array. 4993 * 4994 * @type array ...$0 { 4995 * Array of personal data exporters. 4996 * 4997 * @type callable $callback Callable exporter function that accepts an 4998 * email address and a page number and returns an 4999 * array of name => value pairs of personal data. 5000 * @type string $exporter_friendly_name Translated user facing friendly name for the 5001 * exporter. 5002 * } 5003 * } 5004 */ 5005 $exporters = apply_filters( 'wp_privacy_personal_data_exporters', array() ); 5006 5007 if ( ! is_array( $exporters ) ) { 5008 wp_send_json_error( __( 'An exporter has improperly used the registration filter.' ) ); 5009 } 5010 5011 // Do we have any registered exporters? 5012 if ( 0 < count( $exporters ) ) { 5013 if ( $exporter_index < 1 ) { 5014 wp_send_json_error( __( 'Exporter index cannot be negative.' ) ); 5015 } 5016 5017 if ( $exporter_index > count( $exporters ) ) { 5018 wp_send_json_error( __( 'Exporter index is out of range.' ) ); 5019 } 5020 5021 if ( $page < 1 ) { 5022 wp_send_json_error( __( 'Page index cannot be less than one.' ) ); 5023 } 5024 5025 $exporter_keys = array_keys( $exporters ); 5026 $exporter_key = $exporter_keys[ $exporter_index - 1 ]; 5027 $exporter = $exporters[ $exporter_key ]; 5028 5029 if ( ! is_array( $exporter ) ) { 5030 wp_send_json_error( 5031 /* translators: %s: Exporter array index. */ 5032 sprintf( __( 'Expected an array describing the exporter at index %s.' ), $exporter_key ) 5033 ); 5034 } 5035 5036 if ( ! array_key_exists( 'exporter_friendly_name', $exporter ) ) { 5037 wp_send_json_error( 5038 /* translators: %s: Exporter array index. */ 5039 sprintf( __( 'Exporter array at index %s does not include a friendly name.' ), $exporter_key ) 5040 ); 5041 } 5042 5043 $exporter_friendly_name = $exporter['exporter_friendly_name']; 5044 5045 if ( ! array_key_exists( 'callback', $exporter ) ) { 5046 wp_send_json_error( 5047 /* translators: %s: Exporter friendly name. */ 5048 sprintf( __( 'Exporter does not include a callback: %s.' ), esc_html( $exporter_friendly_name ) ) 5049 ); 5050 } 5051 5052 if ( ! is_callable( $exporter['callback'] ) ) { 5053 wp_send_json_error( 5054 /* translators: %s: Exporter friendly name. */ 5055 sprintf( __( 'Exporter callback is not a valid callback: %s.' ), esc_html( $exporter_friendly_name ) ) 5056 ); 5057 } 5058 5059 $callback = $exporter['callback']; 5060 $response = call_user_func( $callback, $email_address, $page ); 5061 5062 if ( is_wp_error( $response ) ) { 5063 wp_send_json_error( $response ); 5064 } 5065 5066 if ( ! is_array( $response ) ) { 5067 wp_send_json_error( 5068 /* translators: %s: Exporter friendly name. */ 5069 sprintf( __( 'Expected response as an array from exporter: %s.' ), esc_html( $exporter_friendly_name ) ) 5070 ); 5071 } 5072 5073 if ( ! array_key_exists( 'data', $response ) ) { 5074 wp_send_json_error( 5075 /* translators: %s: Exporter friendly name. */ 5076 sprintf( __( 'Expected data in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) ) 5077 ); 5078 } 5079 5080 if ( ! is_array( $response['data'] ) ) { 5081 wp_send_json_error( 5082 /* translators: %s: Exporter friendly name. */ 5083 sprintf( __( 'Expected data array in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) ) 5084 ); 5085 } 5086 5087 if ( ! array_key_exists( 'done', $response ) ) { 5088 wp_send_json_error( 5089 /* translators: %s: Exporter friendly name. */ 5090 sprintf( __( 'Expected done (boolean) in response array from exporter: %s.' ), esc_html( $exporter_friendly_name ) ) 5091 ); 5092 } 5093 } else { 5094 // No exporters, so we're done. 5095 $exporter_key = ''; 5096 5097 $response = array( 5098 'data' => array(), 5099 'done' => true, 5100 ); 5101 } 5102 5103 /** 5104 * Filters a page of personal data exporter data. Used to build the export report. 5105 * 5106 * Allows the export response to be consumed by destinations in addition to Ajax. 5107 * 5108 * @since 4.9.6 5109 * 5110 * @param array $response The personal data for the given exporter and page number. 5111 * @param int $exporter_index The index of the exporter that provided this data. 5112 * @param string $email_address The email address associated with this personal data. 5113 * @param int $page The page number for this response. 5114 * @param int $request_id The privacy request post ID associated with this request. 5115 * @param bool $send_as_email Whether the final results of the export should be emailed to the user. 5116 * @param string $exporter_key The key (slug) of the exporter that provided this data. 5117 */ 5118 $response = apply_filters( 'wp_privacy_personal_data_export_page', $response, $exporter_index, $email_address, $page, $request_id, $send_as_email, $exporter_key ); 5119 5120 if ( is_wp_error( $response ) ) { 5121 wp_send_json_error( $response ); 5122 } 5123 5124 wp_send_json_success( $response ); 5125 } 5126 5127 /** 5128 * Handles erasing personal data via AJAX. 5129 * 5130 * @since 4.9.6 5131 */ 5132 function wp_ajax_wp_privacy_erase_personal_data() { 5133 5134 if ( empty( $_POST['id'] ) ) { 5135 wp_send_json_error( __( 'Missing request ID.' ) ); 5136 } 5137 5138 $request_id = (int) $_POST['id']; 5139 5140 if ( $request_id < 1 ) { 5141 wp_send_json_error( __( 'Invalid request ID.' ) ); 5142 } 5143 5144 // Both capabilities are required to avoid confusion, see `_wp_personal_data_removal_page()`. 5145 if ( ! current_user_can( 'erase_others_personal_data' ) || ! current_user_can( 'delete_users' ) ) { 5146 wp_send_json_error( __( 'Sorry, you are not allowed to perform this action.' ) ); 5147 } 5148 5149 check_ajax_referer( 'wp-privacy-erase-personal-data-' . $request_id, 'security' ); 5150 5151 // Get the request. 5152 $request = wp_get_user_request( $request_id ); 5153 5154 if ( ! $request || 'remove_personal_data' !== $request->action_name ) { 5155 wp_send_json_error( __( 'Invalid request type.' ) ); 5156 } 5157 5158 $email_address = $request->email; 5159 5160 if ( ! is_email( $email_address ) ) { 5161 wp_send_json_error( __( 'Invalid email address in request.' ) ); 5162 } 5163 5164 if ( ! isset( $_POST['eraser'] ) ) { 5165 wp_send_json_error( __( 'Missing eraser index.' ) ); 5166 } 5167 5168 $eraser_index = (int) $_POST['eraser']; 5169 5170 if ( ! isset( $_POST['page'] ) ) { 5171 wp_send_json_error( __( 'Missing page index.' ) ); 5172 } 5173 5174 $page = (int) $_POST['page']; 5175 5176 /** 5177 * Filters the array of personal data eraser callbacks. 5178 * 5179 * @since 4.9.6 5180 * 5181 * @param array $args { 5182 * An array of callable erasers of personal data. Default empty array. 5183 * 5184 * @type array ...$0 { 5185 * Array of personal data exporters. 5186 * 5187 * @type callable $callback Callable eraser that accepts an email address and a page 5188 * number, and returns an array with boolean values for 5189 * whether items were removed or retained and any messages 5190 * from the eraser, as well as if additional pages are 5191 * available. 5192 * @type string $exporter_friendly_name Translated user facing friendly name for the eraser. 5193 * } 5194 * } 5195 */ 5196 $erasers = apply_filters( 'wp_privacy_personal_data_erasers', array() ); 5197 5198 // Do we have any registered erasers? 5199 if ( 0 < count( $erasers ) ) { 5200 5201 if ( $eraser_index < 1 ) { 5202 wp_send_json_error( __( 'Eraser index cannot be less than one.' ) ); 5203 } 5204 5205 if ( $eraser_index > count( $erasers ) ) { 5206 wp_send_json_error( __( 'Eraser index is out of range.' ) ); 5207 } 5208 5209 if ( $page < 1 ) { 5210 wp_send_json_error( __( 'Page index cannot be less than one.' ) ); 5211 } 5212 5213 $eraser_keys = array_keys( $erasers ); 5214 $eraser_key = $eraser_keys[ $eraser_index - 1 ]; 5215 $eraser = $erasers[ $eraser_key ]; 5216 5217 if ( ! is_array( $eraser ) ) { 5218 /* translators: %d: Eraser array index. */ 5219 wp_send_json_error( sprintf( __( 'Expected an array describing the eraser at index %d.' ), $eraser_index ) ); 5220 } 5221 5222 if ( ! array_key_exists( 'eraser_friendly_name', $eraser ) ) { 5223 /* translators: %d: Eraser array index. */ 5224 wp_send_json_error( sprintf( __( 'Eraser array at index %d does not include a friendly name.' ), $eraser_index ) ); 5225 } 5226 5227 $eraser_friendly_name = $eraser['eraser_friendly_name']; 5228 5229 if ( ! array_key_exists( 'callback', $eraser ) ) { 5230 wp_send_json_error( 5231 sprintf( 5232 /* translators: %s: Eraser friendly name. */ 5233 __( 'Eraser does not include a callback: %s.' ), 5234 esc_html( $eraser_friendly_name ) 5235 ) 5236 ); 5237 } 5238 5239 if ( ! is_callable( $eraser['callback'] ) ) { 5240 wp_send_json_error( 5241 sprintf( 5242 /* translators: %s: Eraser friendly name. */ 5243 __( 'Eraser callback is not valid: %s.' ), 5244 esc_html( $eraser_friendly_name ) 5245 ) 5246 ); 5247 } 5248 5249 $callback = $eraser['callback']; 5250 $response = call_user_func( $callback, $email_address, $page ); 5251 5252 if ( is_wp_error( $response ) ) { 5253 wp_send_json_error( $response ); 5254 } 5255 5256 if ( ! is_array( $response ) ) { 5257 wp_send_json_error( 5258 sprintf( 5259 /* translators: 1: Eraser friendly name, 2: Eraser array index. */ 5260 __( 'Did not receive array from %1$s eraser (index %2$d).' ), 5261 esc_html( $eraser_friendly_name ), 5262 $eraser_index 5263 ) 5264 ); 5265 } 5266 5267 if ( ! array_key_exists( 'items_removed', $response ) ) { 5268 wp_send_json_error( 5269 sprintf( 5270 /* translators: 1: Eraser friendly name, 2: Eraser array index. */ 5271 __( 'Expected items_removed key in response array from %1$s eraser (index %2$d).' ), 5272 esc_html( $eraser_friendly_name ), 5273 $eraser_index 5274 ) 5275 ); 5276 } 5277 5278 if ( ! array_key_exists( 'items_retained', $response ) ) { 5279 wp_send_json_error( 5280 sprintf( 5281 /* translators: 1: Eraser friendly name, 2: Eraser array index. */ 5282 __( 'Expected items_retained key in response array from %1$s eraser (index %2$d).' ), 5283 esc_html( $eraser_friendly_name ), 5284 $eraser_index 5285 ) 5286 ); 5287 } 5288 5289 if ( ! array_key_exists( 'messages', $response ) ) { 5290 wp_send_json_error( 5291 sprintf( 5292 /* translators: 1: Eraser friendly name, 2: Eraser array index. */ 5293 __( 'Expected messages key in response array from %1$s eraser (index %2$d).' ), 5294 esc_html( $eraser_friendly_name ), 5295 $eraser_index 5296 ) 5297 ); 5298 } 5299 5300 if ( ! is_array( $response['messages'] ) ) { 5301 wp_send_json_error( 5302 sprintf( 5303 /* translators: 1: Eraser friendly name, 2: Eraser array index. */ 5304 __( 'Expected messages key to reference an array in response array from %1$s eraser (index %2$d).' ), 5305 esc_html( $eraser_friendly_name ), 5306 $eraser_index 5307 ) 5308 ); 5309 } 5310 5311 if ( ! array_key_exists( 'done', $response ) ) { 5312 wp_send_json_error( 5313 sprintf( 5314 /* translators: 1: Eraser friendly name, 2: Eraser array index. */ 5315 __( 'Expected done flag in response array from %1$s eraser (index %2$d).' ), 5316 esc_html( $eraser_friendly_name ), 5317 $eraser_index 5318 ) 5319 ); 5320 } 5321 } else { 5322 // No erasers, so we're done. 5323 $eraser_key = ''; 5324 5325 $response = array( 5326 'items_removed' => false, 5327 'items_retained' => false, 5328 'messages' => array(), 5329 'done' => true, 5330 ); 5331 } 5332 5333 /** 5334 * Filters a page of personal data eraser data. 5335 * 5336 * Allows the erasure response to be consumed by destinations in addition to Ajax. 5337 * 5338 * @since 4.9.6 5339 * 5340 * @param array $response { 5341 * The personal data for the given exporter and page number. 5342 * 5343 * @type bool $items_removed Whether items were actually removed or not. 5344 * @type bool $items_retained Whether items were retained or not. 5345 * @type string[] $messages An array of messages to add to the personal data export file. 5346 * @type bool $done Whether the eraser is finished or not. 5347 * } 5348 * @param int $eraser_index The index of the eraser that provided this data. 5349 * @param string $email_address The email address associated with this personal data. 5350 * @param int $page The page number for this response. 5351 * @param int $request_id The privacy request post ID associated with this request. 5352 * @param string $eraser_key The key (slug) of the eraser that provided this data. 5353 */ 5354 $response = apply_filters( 'wp_privacy_personal_data_erasure_page', $response, $eraser_index, $email_address, $page, $request_id, $eraser_key ); 5355 5356 if ( is_wp_error( $response ) ) { 5357 wp_send_json_error( $response ); 5358 } 5359 5360 wp_send_json_success( $response ); 5361 } 5362 5363 /** 5364 * Handles site health checks on server communication via AJAX. 5365 * 5366 * @since 5.2.0 5367 * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_dotorg_communication() 5368 * @see WP_REST_Site_Health_Controller::test_dotorg_communication() 5369 */ 5370 function wp_ajax_health_check_dotorg_communication() { 5371 _doing_it_wrong( 5372 'wp_ajax_health_check_dotorg_communication', 5373 sprintf( 5374 /* translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it. */ 5375 __( 'The Site Health check for %1$s has been replaced with %2$s.' ), 5376 'wp_ajax_health_check_dotorg_communication', 5377 'WP_REST_Site_Health_Controller::test_dotorg_communication' 5378 ), 5379 '5.6.0' 5380 ); 5381 5382 check_ajax_referer( 'health-check-site-status' ); 5383 5384 if ( ! current_user_can( 'view_site_health_checks' ) ) { 5385 wp_send_json_error(); 5386 } 5387 5388 if ( ! class_exists( 'WP_Site_Health' ) ) { 5389 require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php'; 5390 } 5391 5392 $site_health = WP_Site_Health::get_instance(); 5393 wp_send_json_success( $site_health->get_test_dotorg_communication() ); 5394 } 5395 5396 /** 5397 * Handles site health checks on background updates via AJAX. 5398 * 5399 * @since 5.2.0 5400 * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_background_updates() 5401 * @see WP_REST_Site_Health_Controller::test_background_updates() 5402 */ 5403 function wp_ajax_health_check_background_updates() { 5404 _doing_it_wrong( 5405 'wp_ajax_health_check_background_updates', 5406 sprintf( 5407 /* translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it. */ 5408 __( 'The Site Health check for %1$s has been replaced with %2$s.' ), 5409 'wp_ajax_health_check_background_updates', 5410 'WP_REST_Site_Health_Controller::test_background_updates' 5411 ), 5412 '5.6.0' 5413 ); 5414 5415 check_ajax_referer( 'health-check-site-status' ); 5416 5417 if ( ! current_user_can( 'view_site_health_checks' ) ) { 5418 wp_send_json_error(); 5419 } 5420 5421 if ( ! class_exists( 'WP_Site_Health' ) ) { 5422 require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php'; 5423 } 5424 5425 $site_health = WP_Site_Health::get_instance(); 5426 wp_send_json_success( $site_health->get_test_background_updates() ); 5427 } 5428 5429 /** 5430 * Handles site health checks on loopback requests via AJAX. 5431 * 5432 * @since 5.2.0 5433 * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_loopback_requests() 5434 * @see WP_REST_Site_Health_Controller::test_loopback_requests() 5435 */ 5436 function wp_ajax_health_check_loopback_requests() { 5437 _doing_it_wrong( 5438 'wp_ajax_health_check_loopback_requests', 5439 sprintf( 5440 /* translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it. */ 5441 __( 'The Site Health check for %1$s has been replaced with %2$s.' ), 5442 'wp_ajax_health_check_loopback_requests', 5443 'WP_REST_Site_Health_Controller::test_loopback_requests' 5444 ), 5445 '5.6.0' 5446 ); 5447 5448 check_ajax_referer( 'health-check-site-status' ); 5449 5450 if ( ! current_user_can( 'view_site_health_checks' ) ) { 5451 wp_send_json_error(); 5452 } 5453 5454 if ( ! class_exists( 'WP_Site_Health' ) ) { 5455 require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php'; 5456 } 5457 5458 $site_health = WP_Site_Health::get_instance(); 5459 wp_send_json_success( $site_health->get_test_loopback_requests() ); 5460 } 5461 5462 /** 5463 * Handles site health check to update the result status via AJAX. 5464 * 5465 * @since 5.2.0 5466 */ 5467 function wp_ajax_health_check_site_status_result() { 5468 check_ajax_referer( 'health-check-site-status-result' ); 5469 5470 if ( ! current_user_can( 'view_site_health_checks' ) ) { 5471 wp_send_json_error(); 5472 } 5473 5474 set_transient( 'health-check-site-status-result', wp_json_encode( $_POST['counts'] ) ); 5475 5476 wp_send_json_success(); 5477 } 5478 5479 /** 5480 * Handles site health check to get directories and database sizes via AJAX. 5481 * 5482 * @since 5.2.0 5483 * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::get_directory_sizes() 5484 * @see WP_REST_Site_Health_Controller::get_directory_sizes() 5485 */ 5486 function wp_ajax_health_check_get_sizes() { 5487 _doing_it_wrong( 5488 'wp_ajax_health_check_get_sizes', 5489 sprintf( 5490 /* translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it. */ 5491 __( 'The Site Health check for %1$s has been replaced with %2$s.' ), 5492 'wp_ajax_health_check_get_sizes', 5493 'WP_REST_Site_Health_Controller::get_directory_sizes' 5494 ), 5495 '5.6.0' 5496 ); 5497 5498 check_ajax_referer( 'health-check-site-status-result' ); 5499 5500 if ( ! current_user_can( 'view_site_health_checks' ) || is_multisite() ) { 5501 wp_send_json_error(); 5502 } 5503 5504 if ( ! class_exists( 'WP_Debug_Data' ) ) { 5505 require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php'; 5506 } 5507 5508 $sizes_data = WP_Debug_Data::get_sizes(); 5509 $all_sizes = array( 'raw' => 0 ); 5510 5511 foreach ( $sizes_data as $name => $value ) { 5512 $name = sanitize_text_field( $name ); 5513 $data = array(); 5514 5515 if ( isset( $value['size'] ) ) { 5516 if ( is_string( $value['size'] ) ) { 5517 $data['size'] = sanitize_text_field( $value['size'] ); 5518 } else { 5519 $data['size'] = (int) $value['size']; 5520 } 5521 } 5522 5523 if ( isset( $value['debug'] ) ) { 5524 if ( is_string( $value['debug'] ) ) { 5525 $data['debug'] = sanitize_text_field( $value['debug'] ); 5526 } else { 5527 $data['debug'] = (int) $value['debug']; 5528 } 5529 } 5530 5531 if ( ! empty( $value['raw'] ) ) { 5532 $data['raw'] = (int) $value['raw']; 5533 } 5534 5535 $all_sizes[ $name ] = $data; 5536 } 5537 5538 if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) { 5539 wp_send_json_error( $all_sizes ); 5540 } 5541 5542 wp_send_json_success( $all_sizes ); 5543 } 5544 5545 /** 5546 * Handles renewing the REST API nonce via AJAX. 5547 * 5548 * @since 5.3.0 5549 */ 5550 function wp_ajax_rest_nonce() { 5551 exit( wp_create_nonce( 'wp_rest' ) ); 5552 } 5553 5554 /** 5555 * Handles enabling or disable plugin and theme auto-updates via AJAX. 5556 * 5557 * @since 5.5.0 5558 */ 5559 function wp_ajax_toggle_auto_updates() { 5560 check_ajax_referer( 'updates' ); 5561 5562 if ( empty( $_POST['type'] ) || empty( $_POST['asset'] ) || empty( $_POST['state'] ) ) { 5563 wp_send_json_error( array( 'error' => __( 'Invalid data. No selected item.' ) ) ); 5564 } 5565 5566 $asset = sanitize_text_field( urldecode( $_POST['asset'] ) ); 5567 5568 if ( 'enable' !== $_POST['state'] && 'disable' !== $_POST['state'] ) { 5569 wp_send_json_error( array( 'error' => __( 'Invalid data. Unknown state.' ) ) ); 5570 } 5571 $state = $_POST['state']; 5572 5573 if ( 'plugin' !== $_POST['type'] && 'theme' !== $_POST['type'] ) { 5574 wp_send_json_error( array( 'error' => __( 'Invalid data. Unknown type.' ) ) ); 5575 } 5576 $type = $_POST['type']; 5577 5578 switch ( $type ) { 5579 case 'plugin': 5580 if ( ! current_user_can( 'update_plugins' ) ) { 5581 $error_message = __( 'Sorry, you are not allowed to modify plugins.' ); 5582 wp_send_json_error( array( 'error' => $error_message ) ); 5583 } 5584 5585 $option = 'auto_update_plugins'; 5586 /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ 5587 $all_items = apply_filters( 'all_plugins', get_plugins() ); 5588 break; 5589 case 'theme': 5590 if ( ! current_user_can( 'update_themes' ) ) { 5591 $error_message = __( 'Sorry, you are not allowed to modify themes.' ); 5592 wp_send_json_error( array( 'error' => $error_message ) ); 5593 } 5594 5595 $option = 'auto_update_themes'; 5596 $all_items = wp_get_themes(); 5597 break; 5598 default: 5599 wp_send_json_error( array( 'error' => __( 'Invalid data. Unknown type.' ) ) ); 5600 } 5601 5602 if ( ! array_key_exists( $asset, $all_items ) ) { 5603 $error_message = __( 'Invalid data. The item does not exist.' ); 5604 wp_send_json_error( array( 'error' => $error_message ) ); 5605 } 5606 5607 $auto_updates = (array) get_site_option( $option, array() ); 5608 5609 if ( 'disable' === $state ) { 5610 $auto_updates = array_diff( $auto_updates, array( $asset ) ); 5611 } else { 5612 $auto_updates[] = $asset; 5613 $auto_updates = array_unique( $auto_updates ); 5614 } 5615 5616 // Remove items that have been deleted since the site option was last updated. 5617 $auto_updates = array_intersect( $auto_updates, array_keys( $all_items ) ); 5618 5619 update_site_option( $option, $auto_updates ); 5620 5621 wp_send_json_success(); 5622 } 5623 5624 /** 5625 * Handles sending a password reset link via AJAX. 5626 * 5627 * @since 5.7.0 5628 */ 5629 function wp_ajax_send_password_reset() { 5630 5631 // Validate the nonce for this action. 5632 $user_id = isset( $_POST['user_id'] ) ? (int) $_POST['user_id'] : 0; 5633 check_ajax_referer( 'reset-password-for-' . $user_id, 'nonce' ); 5634 5635 // Verify user capabilities. 5636 if ( ! current_user_can( 'edit_user', $user_id ) ) { 5637 wp_send_json_error( __( 'Cannot send password reset, permission denied.' ) ); 5638 } 5639 5640 // Send the password reset link. 5641 $user = get_userdata( $user_id ); 5642 $results = retrieve_password( $user->user_login ); 5643 5644 if ( true === $results ) { 5645 wp_send_json_success( 5646 /* translators: %s: User's display name. */ 5647 sprintf( __( 'A password reset link was emailed to %s.' ), $user->display_name ) 5648 ); 5649 } else { 5650 wp_send_json_error( $results->get_error_message() ); 5651 } 5652 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Dec 1 08:20:01 2026 | Cross-referenced by PHPXref |