wp/wp-admin/includes/ajax-actions.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    10 //
    10 //
    11 // No-privilege Ajax handlers.
    11 // No-privilege Ajax handlers.
    12 //
    12 //
    13 
    13 
    14 /**
    14 /**
    15  * Ajax handler for the Heartbeat API in
    15  * Ajax handler for the Heartbeat API in the no-privilege context.
    16  * the no-privilege context.
       
    17  *
    16  *
    18  * Runs when the user is not logged in.
    17  * Runs when the user is not logged in.
    19  *
    18  *
    20  * @since 3.6.0
    19  * @since 3.6.0
    21  */
    20  */
   160 			'fields'     => 'names',
   159 			'fields'     => 'names',
   161 			'hide_empty' => false,
   160 			'hide_empty' => false,
   162 		)
   161 		)
   163 	);
   162 	);
   164 
   163 
   165 	echo join( "\n", $results );
   164 	echo implode( "\n", $results );
   166 	wp_die();
   165 	wp_die();
   167 }
   166 }
   168 
   167 
   169 /**
   168 /**
   170  * Ajax handler for compression testing.
   169  * Ajax handler for compression testing.
   225  * Ajax handler for image editor previews.
   224  * Ajax handler for image editor previews.
   226  *
   225  *
   227  * @since 3.1.0
   226  * @since 3.1.0
   228  */
   227  */
   229 function wp_ajax_imgedit_preview() {
   228 function wp_ajax_imgedit_preview() {
   230 	$post_id = intval( $_GET['postid'] );
   229 	$post_id = (int) $_GET['postid'];
   231 	if ( empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
   230 	if ( empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
   232 		wp_die( -1 );
   231 		wp_die( -1 );
   233 	}
   232 	}
   234 
   233 
   235 	check_ajax_referer( "image_editor-$post_id" );
   234 	check_ajax_referer( "image_editor-$post_id" );
   373 		 * that corresponds to a latitude/longitude location).
   372 		 * that corresponds to a latitude/longitude location).
   374 		 *
   373 		 *
   375 		 * The location is stored network-wide, so that the user doesn't have to set it on each site.
   374 		 * The location is stored network-wide, so that the user doesn't have to set it on each site.
   376 		 */
   375 		 */
   377 		if ( $ip_changed || $search ) {
   376 		if ( $ip_changed || $search ) {
   378 			update_user_option( $user_id, 'community-events-location', $events['location'], true );
   377 			update_user_meta( $user_id, 'community-events-location', $events['location'] );
   379 		}
   378 		}
   380 
   379 
   381 		wp_send_json_success( $events );
   380 		wp_send_json_success( $events );
   382 	}
   381 	}
   383 }
   382 }
  1732 	if ( ! $user ) {
  1731 	if ( ! $user ) {
  1733 		wp_die( -1 );
  1732 		wp_die( -1 );
  1734 	}
  1733 	}
  1735 
  1734 
  1736 	if ( is_array( $closed ) ) {
  1735 	if ( is_array( $closed ) ) {
  1737 		update_user_option( $user->ID, "closedpostboxes_$page", $closed, true );
  1736 		update_user_meta( $user->ID, "closedpostboxes_$page", $closed );
  1738 	}
  1737 	}
  1739 
  1738 
  1740 	if ( is_array( $hidden ) ) {
  1739 	if ( is_array( $hidden ) ) {
  1741 		// Postboxes that are always shown.
  1740 		// Postboxes that are always shown.
  1742 		$hidden = array_diff( $hidden, array( 'submitdiv', 'linksubmitdiv', 'manage-menu', 'create-menu' ) );
  1741 		$hidden = array_diff( $hidden, array( 'submitdiv', 'linksubmitdiv', 'manage-menu', 'create-menu' ) );
  1743 		update_user_option( $user->ID, "metaboxhidden_$page", $hidden, true );
  1742 		update_user_meta( $user->ID, "metaboxhidden_$page", $hidden );
  1744 	}
  1743 	}
  1745 
  1744 
  1746 	wp_die( 1 );
  1745 	wp_die( 1 );
  1747 }
  1746 }
  1748 
  1747 
  1763 	if ( ! $user ) {
  1762 	if ( ! $user ) {
  1764 		wp_die( -1 );
  1763 		wp_die( -1 );
  1765 	}
  1764 	}
  1766 
  1765 
  1767 	$hidden = ! empty( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array();
  1766 	$hidden = ! empty( $_POST['hidden'] ) ? explode( ',', $_POST['hidden'] ) : array();
  1768 	update_user_option( $user->ID, "manage{$page}columnshidden", $hidden, true );
  1767 	update_user_meta( $user->ID, "manage{$page}columnshidden", $hidden );
  1769 
  1768 
  1770 	wp_die( 1 );
  1769 	wp_die( 1 );
  1771 }
  1770 }
  1772 
  1771 
  1773 /**
  1772 /**
  1918 	if ( ! $user ) {
  1917 	if ( ! $user ) {
  1919 		wp_die( -1 );
  1918 		wp_die( -1 );
  1920 	}
  1919 	}
  1921 
  1920 
  1922 	if ( $order ) {
  1921 	if ( $order ) {
  1923 		update_user_option( $user->ID, "meta-box-order_$page", $order, true );
  1922 		update_user_meta( $user->ID, "meta-box-order_$page", $order );
  1924 	}
  1923 	}
  1925 
  1924 
  1926 	if ( $page_columns ) {
  1925 	if ( $page_columns ) {
  1927 		update_user_option( $user->ID, "screen_layout_$page", $page_columns, true );
  1926 		update_user_meta( $user->ID, "screen_layout_$page", $page_columns );
  1928 	}
  1927 	}
  1929 
  1928 
  1930 	wp_send_json_success();
  1929 	wp_send_json_success();
  1931 }
  1930 }
  1932 
  1931 
  1952  *
  1951  *
  1953  * @since 3.1.0
  1952  * @since 3.1.0
  1954  */
  1953  */
  1955 function wp_ajax_get_permalink() {
  1954 function wp_ajax_get_permalink() {
  1956 	check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
  1955 	check_ajax_referer( 'getpermalink', 'getpermalinknonce' );
  1957 	$post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
  1956 	$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
  1958 	wp_die( get_preview_post_link( $post_id ) );
  1957 	wp_die( get_preview_post_link( $post_id ) );
  1959 }
  1958 }
  1960 
  1959 
  1961 /**
  1960 /**
  1962  * Ajax handler to retrieve a sample permalink.
  1961  * Ajax handler to retrieve a sample permalink.
  1963  *
  1962  *
  1964  * @since 3.1.0
  1963  * @since 3.1.0
  1965  */
  1964  */
  1966 function wp_ajax_sample_permalink() {
  1965 function wp_ajax_sample_permalink() {
  1967 	check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
  1966 	check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' );
  1968 	$post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0;
  1967 	$post_id = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : 0;
  1969 	$title   = isset( $_POST['new_title'] ) ? $_POST['new_title'] : '';
  1968 	$title   = isset( $_POST['new_title'] ) ? $_POST['new_title'] : '';
  1970 	$slug    = isset( $_POST['new_slug'] ) ? $_POST['new_slug'] : null;
  1969 	$slug    = isset( $_POST['new_slug'] ) ? $_POST['new_slug'] : null;
  1971 	wp_die( get_sample_permalink_html( $post_id, $title, $slug ) );
  1970 	wp_die( get_sample_permalink_html( $post_id, $title, $slug ) );
  1972 }
  1971 }
  1973 
  1972 
  2203 		}
  2202 		}
  2204 
  2203 
  2205 		if ( '0000-00-00 00:00:00' === $post->post_date ) {
  2204 		if ( '0000-00-00 00:00:00' === $post->post_date ) {
  2206 			$time = '';
  2205 			$time = '';
  2207 		} else {
  2206 		} else {
  2208 			/* translators: Date format in table columns, see https://www.php.net/date */
  2207 			/* translators: Date format in table columns, see https://www.php.net/manual/datetime.format.php */
  2209 			$time = mysql2date( __( 'Y/m/d' ), $post->post_date );
  2208 			$time = mysql2date( __( 'Y/m/d' ), $post->post_date );
  2210 		}
  2209 		}
  2211 
  2210 
  2212 		$html .= '<tr class="' . trim( 'found-posts ' . $alt ) . '"><td class="found-radio"><input type="radio" id="found-' . $post->ID . '" name="found_post_id" value="' . esc_attr( $post->ID ) . '"></td>';
  2211 		$html .= '<tr class="' . trim( 'found-posts ' . $alt ) . '"><td class="found-radio"><input type="radio" id="found-' . $post->ID . '" name="found_post_id" value="' . esc_attr( $post->ID ) . '"></td>';
  2213 		$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";
  2212 		$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";
  2368 
  2367 
  2369 	wp_die();
  2368 	wp_die();
  2370 }
  2369 }
  2371 
  2370 
  2372 /**
  2371 /**
  2373  * Ajax handler for saving a widget.
  2372  * Ajax handler for updating a widget.
  2374  *
  2373  *
  2375  * @since 3.9.0
  2374  * @since 3.9.0
  2376  *
  2375  *
  2377  * @global WP_Customize_Manager $wp_customize
  2376  * @global WP_Customize_Manager $wp_customize
  2378  */
  2377  */
  2592  * Ajax handler for image editing.
  2591  * Ajax handler for image editing.
  2593  *
  2592  *
  2594  * @since 3.1.0
  2593  * @since 3.1.0
  2595  */
  2594  */
  2596 function wp_ajax_image_editor() {
  2595 function wp_ajax_image_editor() {
  2597 	$attachment_id = intval( $_POST['postid'] );
  2596 	$attachment_id = (int) $_POST['postid'];
  2598 
  2597 
  2599 	if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) {
  2598 	if ( empty( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) {
  2600 		wp_die( -1 );
  2599 		wp_die( -1 );
  2601 	}
  2600 	}
  2602 
  2601 
  2603 	check_ajax_referer( "image_editor-$attachment_id" );
  2602 	check_ajax_referer( "image_editor-$attachment_id" );
  2604 	include_once ABSPATH . 'wp-admin/includes/image-edit.php';
  2603 	include_once ABSPATH . 'wp-admin/includes/image-edit.php';
  2605 
  2604 
  2606 	$msg = false;
  2605 	$msg = false;
       
  2606 
  2607 	switch ( $_POST['do'] ) {
  2607 	switch ( $_POST['do'] ) {
  2608 		case 'save':
  2608 		case 'save':
  2609 			$msg = wp_save_image( $attachment_id );
  2609 			$msg = wp_save_image( $attachment_id );
  2610 			if ( $msg->error ) {
  2610 			if ( ! empty( $msg->error ) ) {
  2611 				wp_send_json_error( $msg );
  2611 				wp_send_json_error( $msg );
  2612 			}
  2612 			}
  2613 
  2613 
  2614 			wp_send_json_success( $msg );
  2614 			wp_send_json_success( $msg );
  2615 			break;
  2615 			break;
  2623 
  2623 
  2624 	ob_start();
  2624 	ob_start();
  2625 	wp_image_editor( $attachment_id, $msg );
  2625 	wp_image_editor( $attachment_id, $msg );
  2626 	$html = ob_get_clean();
  2626 	$html = ob_get_clean();
  2627 
  2627 
  2628 	if ( $msg->error ) {
  2628 	if ( ! empty( $msg->error ) ) {
  2629 		wp_send_json_error(
  2629 		wp_send_json_error(
  2630 			array(
  2630 			array(
  2631 				'message' => $msg,
  2631 				'message' => $msg,
  2632 				'html'    => $html,
  2632 				'html'    => $html,
  2633 			)
  2633 			)
  2648  * @since 3.1.0
  2648  * @since 3.1.0
  2649  */
  2649  */
  2650 function wp_ajax_set_post_thumbnail() {
  2650 function wp_ajax_set_post_thumbnail() {
  2651 	$json = ! empty( $_REQUEST['json'] ); // New-style request.
  2651 	$json = ! empty( $_REQUEST['json'] ); // New-style request.
  2652 
  2652 
  2653 	$post_ID = intval( $_POST['post_id'] );
  2653 	$post_ID = (int) $_POST['post_id'];
  2654 	if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  2654 	if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  2655 		wp_die( -1 );
  2655 		wp_die( -1 );
  2656 	}
  2656 	}
  2657 
  2657 
  2658 	$thumbnail_id = intval( $_POST['thumbnail_id'] );
  2658 	$thumbnail_id = (int) $_POST['thumbnail_id'];
  2659 
  2659 
  2660 	if ( $json ) {
  2660 	if ( $json ) {
  2661 		check_ajax_referer( "update-post_$post_ID" );
  2661 		check_ajax_referer( "update-post_$post_ID" );
  2662 	} else {
  2662 	} else {
  2663 		check_ajax_referer( "set_post_thumbnail-$post_ID" );
  2663 		check_ajax_referer( "set_post_thumbnail-$post_ID" );
  2684  * Ajax handler for retrieving HTML for the featured image.
  2684  * Ajax handler for retrieving HTML for the featured image.
  2685  *
  2685  *
  2686  * @since 4.6.0
  2686  * @since 4.6.0
  2687  */
  2687  */
  2688 function wp_ajax_get_post_thumbnail_html() {
  2688 function wp_ajax_get_post_thumbnail_html() {
  2689 	$post_ID = intval( $_POST['post_id'] );
  2689 	$post_ID = (int) $_POST['post_id'];
  2690 
  2690 
  2691 	check_ajax_referer( "update-post_$post_ID" );
  2691 	check_ajax_referer( "update-post_$post_ID" );
  2692 
  2692 
  2693 	if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  2693 	if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  2694 		wp_die( -1 );
  2694 		wp_die( -1 );
  2695 	}
  2695 	}
  2696 
  2696 
  2697 	$thumbnail_id = intval( $_POST['thumbnail_id'] );
  2697 	$thumbnail_id = (int) $_POST['thumbnail_id'];
  2698 
  2698 
  2699 	// For backward compatibility, -1 refers to no featured image.
  2699 	// For backward compatibility, -1 refers to no featured image.
  2700 	if ( -1 === $thumbnail_id ) {
  2700 	if ( -1 === $thumbnail_id ) {
  2701 		$thumbnail_id = null;
  2701 		$thumbnail_id = null;
  2702 	}
  2702 	}
  2985 	 *
  2985 	 *
  2986 	 * @see WP_Query::parse_query()
  2986 	 * @see WP_Query::parse_query()
  2987 	 *
  2987 	 *
  2988 	 * @param array $query An array of query variables.
  2988 	 * @param array $query An array of query variables.
  2989 	 */
  2989 	 */
  2990 	$query = apply_filters( 'ajax_query_attachments_args', $query );
  2990 	$query             = apply_filters( 'ajax_query_attachments_args', $query );
  2991 	$query = new WP_Query( $query );
  2991 	$attachments_query = new WP_Query( $query );
  2992 
  2992 
  2993 	$posts = array_map( 'wp_prepare_attachment_for_js', $query->posts );
  2993 	$posts       = array_map( 'wp_prepare_attachment_for_js', $attachments_query->posts );
  2994 	$posts = array_filter( $posts );
  2994 	$posts       = array_filter( $posts );
       
  2995 	$total_posts = $attachments_query->found_posts;
       
  2996 
       
  2997 	if ( $total_posts < 1 ) {
       
  2998 		// Out-of-bounds, run the query again without LIMIT for total count.
       
  2999 		unset( $query['paged'] );
       
  3000 
       
  3001 		$count_query = new WP_Query();
       
  3002 		$count_query->query( $query );
       
  3003 		$total_posts = $count_query->found_posts;
       
  3004 	}
       
  3005 
       
  3006 	$posts_per_page = (int) $attachments_query->query['posts_per_page'];
       
  3007 
       
  3008 	$max_pages = $posts_per_page ? ceil( $total_posts / $posts_per_page ) : 0;
       
  3009 
       
  3010 	header( 'X-WP-Total: ' . (int) $total_posts );
       
  3011 	header( 'X-WP-TotalPages: ' . (int) $max_pages );
  2995 
  3012 
  2996 	wp_send_json_success( $posts );
  3013 	wp_send_json_success( $posts );
  2997 }
  3014 }
  2998 
  3015 
  2999 /**
  3016 /**
  3205 function wp_ajax_send_attachment_to_editor() {
  3222 function wp_ajax_send_attachment_to_editor() {
  3206 	check_ajax_referer( 'media-send-to-editor', 'nonce' );
  3223 	check_ajax_referer( 'media-send-to-editor', 'nonce' );
  3207 
  3224 
  3208 	$attachment = wp_unslash( $_POST['attachment'] );
  3225 	$attachment = wp_unslash( $_POST['attachment'] );
  3209 
  3226 
  3210 	$id = intval( $attachment['id'] );
  3227 	$id = (int) $attachment['id'];
  3211 
  3228 
  3212 	$post = get_post( $id );
  3229 	$post = get_post( $id );
  3213 	if ( ! $post ) {
  3230 	if ( ! $post ) {
  3214 		wp_send_json_error();
  3231 		wp_send_json_error();
  3215 	}
  3232 	}
  3218 		wp_send_json_error();
  3235 		wp_send_json_error();
  3219 	}
  3236 	}
  3220 
  3237 
  3221 	if ( current_user_can( 'edit_post', $id ) ) {
  3238 	if ( current_user_can( 'edit_post', $id ) ) {
  3222 		// If this attachment is unattached, attach it. Primarily a back compat thing.
  3239 		// If this attachment is unattached, attach it. Primarily a back compat thing.
  3223 		$insert_into_post_id = intval( $_POST['post_id'] );
  3240 		$insert_into_post_id = (int) $_POST['post_id'];
  3224 
  3241 
  3225 		if ( 0 == $post->post_parent && $insert_into_post_id ) {
  3242 		if ( 0 == $post->post_parent && $insert_into_post_id ) {
  3226 			wp_update_post(
  3243 			wp_update_post(
  3227 				array(
  3244 				array(
  3228 					'ID'          => $id,
  3245 					'ID'          => $id,
  3619 
  3636 
  3620 	if ( empty( $_POST['shortcode'] ) ) {
  3637 	if ( empty( $_POST['shortcode'] ) ) {
  3621 		wp_send_json_error();
  3638 		wp_send_json_error();
  3622 	}
  3639 	}
  3623 
  3640 
  3624 	$post_id = isset( $_POST['post_ID'] ) ? intval( $_POST['post_ID'] ) : 0;
  3641 	$post_id = isset( $_POST['post_ID'] ) ? (int) $_POST['post_ID'] : 0;
  3625 
  3642 
  3626 	if ( $post_id > 0 ) {
  3643 	if ( $post_id > 0 ) {
  3627 		$post = get_post( $post_id );
  3644 		$post = get_post( $post_id );
  3628 
  3645 
  3629 		if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
  3646 		if ( ! $post || ! current_user_can( 'edit_post', $post->ID ) ) {
  3671 	}
  3688 	}
  3672 
  3689 
  3673 	// Set $content_width so any embeds fit in the destination iframe.
  3690 	// Set $content_width so any embeds fit in the destination iframe.
  3674 	if ( isset( $_POST['maxwidth'] ) && is_numeric( $_POST['maxwidth'] ) && $_POST['maxwidth'] > 0 ) {
  3691 	if ( isset( $_POST['maxwidth'] ) && is_numeric( $_POST['maxwidth'] ) && $_POST['maxwidth'] > 0 ) {
  3675 		if ( ! isset( $content_width ) ) {
  3692 		if ( ! isset( $content_width ) ) {
  3676 			$content_width = intval( $_POST['maxwidth'] );
  3693 			$content_width = (int) $_POST['maxwidth'];
  3677 		} else {
  3694 		} else {
  3678 			$content_width = min( $content_width, intval( $_POST['maxwidth'] ) );
  3695 			$content_width = min( $content_width, (int) $_POST['maxwidth'] );
  3679 		}
  3696 		}
  3680 	}
  3697 	}
  3681 
  3698 
  3682 	if ( $url && ! $parsed ) {
  3699 	if ( $url && ! $parsed ) {
  3683 		$parsed = $wp_embed->run_shortcode( $shortcode );
  3700 		$parsed = $wp_embed->run_shortcode( $shortcode );
  3696 	if ( has_shortcode( $parsed, 'audio' ) || has_shortcode( $parsed, 'video' ) ) {
  3713 	if ( has_shortcode( $parsed, 'audio' ) || has_shortcode( $parsed, 'video' ) ) {
  3697 		$styles     = '';
  3714 		$styles     = '';
  3698 		$mce_styles = wpview_media_sandbox_styles();
  3715 		$mce_styles = wpview_media_sandbox_styles();
  3699 
  3716 
  3700 		foreach ( $mce_styles as $style ) {
  3717 		foreach ( $mce_styles as $style ) {
  3701 			$styles .= sprintf( '<link rel="stylesheet" href="%s"/>', $style );
  3718 			$styles .= sprintf( '<link rel="stylesheet" href="%s" />', $style );
  3702 		}
  3719 		}
  3703 
  3720 
  3704 		$html = do_shortcode( $parsed );
  3721 		$html = do_shortcode( $parsed );
  3705 
  3722 
  3706 		global $wp_scripts;
  3723 		global $wp_scripts;
  3924 			$cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
  3941 			$cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
  3925 
  3942 
  3926 			$parent_url = wp_get_attachment_url( $attachment_id );
  3943 			$parent_url = wp_get_attachment_url( $attachment_id );
  3927 			$url        = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
  3944 			$url        = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
  3928 
  3945 
  3929 			$size       = @getimagesize( $cropped );
  3946 			$size       = wp_getimagesize( $cropped );
  3930 			$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  3947 			$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
  3931 
  3948 
  3932 			$object = array(
  3949 			$object = array(
  3933 				'post_title'     => wp_basename( $cropped ),
  3950 				'post_title'     => wp_basename( $cropped ),
  3934 				'post_content'   => $url,
  3951 				'post_content'   => $url,
  3970  * Ajax handler for generating a password.
  3987  * Ajax handler for generating a password.
  3971  *
  3988  *
  3972  * @since 4.4.0
  3989  * @since 4.4.0
  3973  */
  3990  */
  3974 function wp_ajax_generate_password() {
  3991 function wp_ajax_generate_password() {
       
  3992 	wp_send_json_success( wp_generate_password( 24 ) );
       
  3993 }
       
  3994 
       
  3995 /**
       
  3996  * Ajax handler for generating a password in the no-privilege context.
       
  3997  *
       
  3998  * @since 5.7.0
       
  3999  */
       
  4000 function wp_ajax_nopriv_generate_password() {
  3975 	wp_send_json_success( wp_generate_password( 24 ) );
  4001 	wp_send_json_success( wp_generate_password( 24 ) );
  3976 }
  4002 }
  3977 
  4003 
  3978 /**
  4004 /**
  3979  * Ajax handler for saving the user's WordPress.org username.
  4005  * Ajax handler for saving the user's WordPress.org username.
  5139 
  5165 
  5140 /**
  5166 /**
  5141  * Ajax handler for site health checks on server communication.
  5167  * Ajax handler for site health checks on server communication.
  5142  *
  5168  *
  5143  * @since 5.2.0
  5169  * @since 5.2.0
       
  5170  * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_dotorg_communication()
       
  5171  * @see WP_REST_Site_Health_Controller::test_dotorg_communication()
  5144  */
  5172  */
  5145 function wp_ajax_health_check_dotorg_communication() {
  5173 function wp_ajax_health_check_dotorg_communication() {
       
  5174 	_doing_it_wrong(
       
  5175 		'wp_ajax_health_check_dotorg_communication',
       
  5176 		sprintf(
       
  5177 		// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
       
  5178 			__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
       
  5179 			'wp_ajax_health_check_dotorg_communication',
       
  5180 			'WP_REST_Site_Health_Controller::test_dotorg_communication'
       
  5181 		),
       
  5182 		'5.6.0'
       
  5183 	);
       
  5184 
  5146 	check_ajax_referer( 'health-check-site-status' );
  5185 	check_ajax_referer( 'health-check-site-status' );
  5147 
  5186 
  5148 	if ( ! current_user_can( 'view_site_health_checks' ) ) {
  5187 	if ( ! current_user_can( 'view_site_health_checks' ) ) {
  5149 		wp_send_json_error();
  5188 		wp_send_json_error();
  5150 	}
  5189 	}
  5156 	$site_health = WP_Site_Health::get_instance();
  5195 	$site_health = WP_Site_Health::get_instance();
  5157 	wp_send_json_success( $site_health->get_test_dotorg_communication() );
  5196 	wp_send_json_success( $site_health->get_test_dotorg_communication() );
  5158 }
  5197 }
  5159 
  5198 
  5160 /**
  5199 /**
  5161  * Ajax handler for site health checks on debug mode.
  5200  * Ajax handler for site health checks on background updates.
  5162  *
  5201  *
  5163  * @since 5.2.0
  5202  * @since 5.2.0
  5164  */
  5203  * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_background_updates()
  5165 function wp_ajax_health_check_is_in_debug_mode() {
  5204  * @see WP_REST_Site_Health_Controller::test_background_updates()
  5166 	wp_verify_nonce( 'health-check-site-status' );
  5205  */
       
  5206 function wp_ajax_health_check_background_updates() {
       
  5207 	_doing_it_wrong(
       
  5208 		'wp_ajax_health_check_background_updates',
       
  5209 		sprintf(
       
  5210 		// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
       
  5211 			__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
       
  5212 			'wp_ajax_health_check_background_updates',
       
  5213 			'WP_REST_Site_Health_Controller::test_background_updates'
       
  5214 		),
       
  5215 		'5.6.0'
       
  5216 	);
       
  5217 
       
  5218 	check_ajax_referer( 'health-check-site-status' );
  5167 
  5219 
  5168 	if ( ! current_user_can( 'view_site_health_checks' ) ) {
  5220 	if ( ! current_user_can( 'view_site_health_checks' ) ) {
  5169 		wp_send_json_error();
  5221 		wp_send_json_error();
  5170 	}
  5222 	}
  5171 
  5223 
  5172 	if ( ! class_exists( 'WP_Site_Health' ) ) {
  5224 	if ( ! class_exists( 'WP_Site_Health' ) ) {
  5173 		require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
  5225 		require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
  5174 	}
  5226 	}
  5175 
  5227 
  5176 	$site_health = WP_Site_Health::get_instance();
  5228 	$site_health = WP_Site_Health::get_instance();
  5177 	wp_send_json_success( $site_health->get_test_is_in_debug_mode() );
  5229 	wp_send_json_success( $site_health->get_test_background_updates() );
  5178 }
  5230 }
  5179 
  5231 
  5180 /**
  5232 /**
  5181  * Ajax handler for site health checks on background updates.
  5233  * Ajax handler for site health checks on loopback requests.
  5182  *
  5234  *
  5183  * @since 5.2.0
  5235  * @since 5.2.0
  5184  */
  5236  * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::test_loopback_requests()
  5185 function wp_ajax_health_check_background_updates() {
  5237  * @see WP_REST_Site_Health_Controller::test_loopback_requests()
       
  5238  */
       
  5239 function wp_ajax_health_check_loopback_requests() {
       
  5240 	_doing_it_wrong(
       
  5241 		'wp_ajax_health_check_loopback_requests',
       
  5242 		sprintf(
       
  5243 		// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
       
  5244 			__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
       
  5245 			'wp_ajax_health_check_loopback_requests',
       
  5246 			'WP_REST_Site_Health_Controller::test_loopback_requests'
       
  5247 		),
       
  5248 		'5.6.0'
       
  5249 	);
       
  5250 
  5186 	check_ajax_referer( 'health-check-site-status' );
  5251 	check_ajax_referer( 'health-check-site-status' );
  5187 
  5252 
  5188 	if ( ! current_user_can( 'view_site_health_checks' ) ) {
  5253 	if ( ! current_user_can( 'view_site_health_checks' ) ) {
  5189 		wp_send_json_error();
  5254 		wp_send_json_error();
  5190 	}
  5255 	}
  5192 	if ( ! class_exists( 'WP_Site_Health' ) ) {
  5257 	if ( ! class_exists( 'WP_Site_Health' ) ) {
  5193 		require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
  5258 		require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
  5194 	}
  5259 	}
  5195 
  5260 
  5196 	$site_health = WP_Site_Health::get_instance();
  5261 	$site_health = WP_Site_Health::get_instance();
  5197 	wp_send_json_success( $site_health->get_test_background_updates() );
  5262 	wp_send_json_success( $site_health->get_test_loopback_requests() );
  5198 }
  5263 }
  5199 
  5264 
  5200 /**
  5265 /**
  5201  * Ajax handler for site health checks on loopback requests.
  5266  * Ajax handler for site health check to update the result status.
  5202  *
  5267  *
  5203  * @since 5.2.0
  5268  * @since 5.2.0
  5204  */
  5269  */
  5205 function wp_ajax_health_check_loopback_requests() {
  5270 function wp_ajax_health_check_site_status_result() {
  5206 	check_ajax_referer( 'health-check-site-status' );
  5271 	check_ajax_referer( 'health-check-site-status-result' );
  5207 
  5272 
  5208 	if ( ! current_user_can( 'view_site_health_checks' ) ) {
  5273 	if ( ! current_user_can( 'view_site_health_checks' ) ) {
  5209 		wp_send_json_error();
  5274 		wp_send_json_error();
  5210 	}
  5275 	}
  5211 
  5276 
  5212 	if ( ! class_exists( 'WP_Site_Health' ) ) {
  5277 	set_transient( 'health-check-site-status-result', wp_json_encode( $_POST['counts'] ) );
  5213 		require_once ABSPATH . 'wp-admin/includes/class-wp-site-health.php';
  5278 
  5214 	}
  5279 	wp_send_json_success();
  5215 
  5280 }
  5216 	$site_health = WP_Site_Health::get_instance();
  5281 
  5217 	wp_send_json_success( $site_health->get_test_loopback_requests() );
  5282 /**
  5218 }
  5283  * Ajax handler for site health check to get directories and database sizes.
  5219 
       
  5220 /**
       
  5221  * Ajax handler for site health check to update the result status.
       
  5222  *
  5284  *
  5223  * @since 5.2.0
  5285  * @since 5.2.0
  5224  */
  5286  * @deprecated 5.6.0 Use WP_REST_Site_Health_Controller::get_directory_sizes()
  5225 function wp_ajax_health_check_site_status_result() {
  5287  * @see WP_REST_Site_Health_Controller::get_directory_sizes()
  5226 	check_ajax_referer( 'health-check-site-status-result' );
       
  5227 
       
  5228 	if ( ! current_user_can( 'view_site_health_checks' ) ) {
       
  5229 		wp_send_json_error();
       
  5230 	}
       
  5231 
       
  5232 	set_transient( 'health-check-site-status-result', wp_json_encode( $_POST['counts'] ) );
       
  5233 
       
  5234 	wp_send_json_success();
       
  5235 }
       
  5236 
       
  5237 /**
       
  5238  * Ajax handler for site health check to get directories and database sizes.
       
  5239  *
       
  5240  * @since 5.2.0
       
  5241  */
  5288  */
  5242 function wp_ajax_health_check_get_sizes() {
  5289 function wp_ajax_health_check_get_sizes() {
       
  5290 	_doing_it_wrong(
       
  5291 		'wp_ajax_health_check_get_sizes',
       
  5292 		sprintf(
       
  5293 		// translators: 1: The Site Health action that is no longer used by core. 2: The new function that replaces it.
       
  5294 			__( 'The Site Health check for %1$s has been replaced with %2$s.' ),
       
  5295 			'wp_ajax_health_check_get_sizes',
       
  5296 			'WP_REST_Site_Health_Controller::get_directory_sizes'
       
  5297 		),
       
  5298 		'5.6.0'
       
  5299 	);
       
  5300 
  5243 	check_ajax_referer( 'health-check-site-status-result' );
  5301 	check_ajax_referer( 'health-check-site-status-result' );
  5244 
  5302 
  5245 	if ( ! current_user_can( 'view_site_health_checks' ) || is_multisite() ) {
  5303 	if ( ! current_user_can( 'view_site_health_checks' ) || is_multisite() ) {
  5246 		wp_send_json_error();
  5304 		wp_send_json_error();
  5247 	}
  5305 	}
  5363 
  5421 
  5364 	update_site_option( $option, $auto_updates );
  5422 	update_site_option( $option, $auto_updates );
  5365 
  5423 
  5366 	wp_send_json_success();
  5424 	wp_send_json_success();
  5367 }
  5425 }
       
  5426 
       
  5427 /**
       
  5428  * Ajax handler sends a password reset link.
       
  5429  *
       
  5430  * @since 5.7.0
       
  5431  */
       
  5432 function wp_ajax_send_password_reset() {
       
  5433 
       
  5434 	// Validate the nonce for this action.
       
  5435 	$user_id = isset( $_POST['user_id'] ) ? (int) $_POST['user_id'] : 0;
       
  5436 	check_ajax_referer( 'reset-password-for-' . $user_id, 'nonce' );
       
  5437 
       
  5438 	// Verify user capabilities.
       
  5439 	if ( ! current_user_can( 'edit_user', $user_id ) ) {
       
  5440 		wp_send_json_error( __( 'Cannot send password reset, permission denied.' ) );
       
  5441 	}
       
  5442 
       
  5443 	// Send the password reset link.
       
  5444 	$user    = get_userdata( $user_id );
       
  5445 	$results = retrieve_password( $user->user_login );
       
  5446 
       
  5447 	if ( true === $results ) {
       
  5448 		wp_send_json_success(
       
  5449 			/* translators: %s: User's display name. */
       
  5450 			sprintf( __( 'A password reset link was emailed to %s.' ), $user->display_name )
       
  5451 		);
       
  5452 	} else {
       
  5453 		wp_send_json_error( $results->get_error_message() );
       
  5454 	}
       
  5455 }