WP_Site_Health::get_test_update_temp_backup_writable()publicWP 6.3.0

Tests if plugin and theme temporary backup directories are writable or can be created.

Method of the class: WP_Site_Health{}

No Hooks.

Return

Array. The test results.

Usage

$WP_Site_Health = new WP_Site_Health();
$WP_Site_Health->get_test_update_temp_backup_writable();

Notes

  • Global. WP_Filesystem_Base. $wp_filesystem WordPress filesystem subclass.

Changelog

Since 6.3.0 Introduced.

WP_Site_Health::get_test_update_temp_backup_writable() code WP 6.6.2

public function get_test_update_temp_backup_writable() {
	global $wp_filesystem;

	$result = array(
		'label'       => __( 'Plugin and theme temporary backup directory is writable' ),
		'status'      => 'good',
		'badge'       => array(
			'label' => __( 'Security' ),
			'color' => 'blue',
		),
		'description' => sprintf(
			/* translators: %s: wp-content/upgrade-temp-backup */
			'<p>' . __( 'The %s directory used to improve the stability of plugin and theme updates is writable.' ) . '</p>',
			'<code>wp-content/upgrade-temp-backup</code>'
		),
		'actions'     => '',
		'test'        => 'update_temp_backup_writable',
	);

	if ( ! function_exists( 'WP_Filesystem' ) ) {
		require_once ABSPATH . '/wp-admin/includes/file.php';
	}

	ob_start();
	$credentials = request_filesystem_credentials( '' );
	ob_end_clean();

	if ( false === $credentials || ! WP_Filesystem( $credentials ) ) {
		$result['status']      = 'recommended';
		$result['label']       = __( 'Could not access filesystem' );
		$result['description'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' );
		return $result;
	}

	$wp_content = $wp_filesystem->wp_content_dir();

	if ( ! $wp_content ) {
		$result['status']      = 'critical';
		$result['label']       = __( 'Unable to locate WordPress content directory' );
		$result['description'] = sprintf(
			/* translators: %s: wp-content */
			'<p>' . __( 'The %s directory cannot be located.' ) . '</p>',
			'<code>wp-content</code>'
		);
		return $result;
	}

	$upgrade_dir_exists      = $wp_filesystem->is_dir( "$wp_content/upgrade" );
	$upgrade_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade" );
	$backup_dir_exists       = $wp_filesystem->is_dir( "$wp_content/upgrade-temp-backup" );
	$backup_dir_is_writable  = $wp_filesystem->is_writable( "$wp_content/upgrade-temp-backup" );

	$plugins_dir_exists      = $wp_filesystem->is_dir( "$wp_content/upgrade-temp-backup/plugins" );
	$plugins_dir_is_writable = $wp_filesystem->is_writable( "$wp_content/upgrade-temp-backup/plugins" );
	$themes_dir_exists       = $wp_filesystem->is_dir( "$wp_content/upgrade-temp-backup/themes" );
	$themes_dir_is_writable  = $wp_filesystem->is_writable( "$wp_content/upgrade-temp-backup/themes" );

	if ( $plugins_dir_exists && ! $plugins_dir_is_writable && $themes_dir_exists && ! $themes_dir_is_writable ) {
		$result['status']      = 'critical';
		$result['label']       = __( 'Plugin and theme temporary backup directories exist but are not writable' );
		$result['description'] = sprintf(
			/* translators: 1: wp-content/upgrade-temp-backup/plugins, 2: wp-content/upgrade-temp-backup/themes. */
			'<p>' . __( 'The %1$s and %2$s directories exist but are not writable. These directories are used to improve the stability of plugin updates. Please make sure the server has write permissions to these directories.' ) . '</p>',
			'<code>wp-content/upgrade-temp-backup/plugins</code>',
			'<code>wp-content/upgrade-temp-backup/themes</code>'
		);
		return $result;
	}

	if ( $plugins_dir_exists && ! $plugins_dir_is_writable ) {
		$result['status']      = 'critical';
		$result['label']       = __( 'Plugin temporary backup directory exists but is not writable' );
		$result['description'] = sprintf(
			/* translators: %s: wp-content/upgrade-temp-backup/plugins */
			'<p>' . __( 'The %s directory exists but is not writable. This directory is used to improve the stability of plugin updates. Please make sure the server has write permissions to this directory.' ) . '</p>',
			'<code>wp-content/upgrade-temp-backup/plugins</code>'
		);
		return $result;
	}

	if ( $themes_dir_exists && ! $themes_dir_is_writable ) {
		$result['status']      = 'critical';
		$result['label']       = __( 'Theme temporary backup directory exists but is not writable' );
		$result['description'] = sprintf(
			/* translators: %s: wp-content/upgrade-temp-backup/themes */
			'<p>' . __( 'The %s directory exists but is not writable. This directory is used to improve the stability of theme updates. Please make sure the server has write permissions to this directory.' ) . '</p>',
			'<code>wp-content/upgrade-temp-backup/themes</code>'
		);
		return $result;
	}

	if ( ( ! $plugins_dir_exists || ! $themes_dir_exists ) && $backup_dir_exists && ! $backup_dir_is_writable ) {
		$result['status']      = 'critical';
		$result['label']       = __( 'The temporary backup directory exists but is not writable' );
		$result['description'] = sprintf(
			/* translators: %s: wp-content/upgrade-temp-backup */
			'<p>' . __( 'The %s directory exists but is not writable. This directory is used to improve the stability of plugin and theme updates. Please make sure the server has write permissions to this directory.' ) . '</p>',
			'<code>wp-content/upgrade-temp-backup</code>'
		);
		return $result;
	}

	if ( ! $backup_dir_exists && $upgrade_dir_exists && ! $upgrade_dir_is_writable ) {
		$result['status']      = 'critical';
		$result['label']       = __( 'The upgrade directory exists but is not writable' );
		$result['description'] = sprintf(
			/* translators: %s: wp-content/upgrade */
			'<p>' . __( 'The %s directory exists but is not writable. This directory is used for plugin and theme updates. Please make sure the server has write permissions to this directory.' ) . '</p>',
			'<code>wp-content/upgrade</code>'
		);
		return $result;
	}

	if ( ! $upgrade_dir_exists && ! $wp_filesystem->is_writable( $wp_content ) ) {
		$result['status']      = 'critical';
		$result['label']       = __( 'The upgrade directory cannot be created' );
		$result['description'] = sprintf(
			/* translators: 1: wp-content/upgrade, 2: wp-content. */
			'<p>' . __( 'The %1$s directory does not exist, and the server does not have write permissions in %2$s to create it. This directory is used for plugin and theme updates. Please make sure the server has write permissions in %2$s.' ) . '</p>',
			'<code>wp-content/upgrade</code>',
			'<code>wp-content</code>'
		);
		return $result;
	}

	return $result;
}