WP_Site_Health_Auto_Updates::test_all_files_writable()publicWP 5.2.0

Checks if core files are writable by the web user/group.

Method of the class: WP_Site_Health_Auto_Updates{}

No Hooks.

Return

Array|false. The test results. False if they're not writeable.

Usage

$WP_Site_Health_Auto_Updates = new WP_Site_Health_Auto_Updates();
$WP_Site_Health_Auto_Updates->test_all_files_writable();

Notes

  • Global. WP_Filesystem_Base. $wp_filesystem WordPress filesystem subclass.

Changelog

Since 5.2.0 Introduced.

WP_Site_Health_Auto_Updates::test_all_files_writable() code WP 6.5.2

public function test_all_files_writable() {
	global $wp_filesystem;

	require ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z

	$skin    = new Automatic_Upgrader_Skin();
	$success = $skin->request_filesystem_credentials( false, ABSPATH );

	if ( ! $success ) {
		return false;
	}

	WP_Filesystem();

	if ( 'direct' !== $wp_filesystem->method ) {
		return false;
	}

	// Make sure the `get_core_checksums()` function is available during our REST API call.
	if ( ! function_exists( 'get_core_checksums' ) ) {
		require_once ABSPATH . 'wp-admin/includes/update.php';
	}

	$checksums = get_core_checksums( $wp_version, 'en_US' );
	$dev       = ( str_contains( $wp_version, '-' ) );
	// Get the last stable version's files and test against that.
	if ( ! $checksums && $dev ) {
		$checksums = get_core_checksums( (float) $wp_version - 0.1, 'en_US' );
	}

	// There aren't always checksums for development releases, so just skip the test if we still can't find any.
	if ( ! $checksums && $dev ) {
		return false;
	}

	if ( ! $checksums ) {
		$description = sprintf(
			/* translators: %s: WordPress version. */
			__( "Couldn't retrieve a list of the checksums for WordPress %s." ),
			$wp_version
		);
		$description .= ' ' . __( 'This could mean that connections are failing to WordPress.org.' );
		return array(
			'description' => $description,
			'severity'    => 'warning',
		);
	}

	$unwritable_files = array();
	foreach ( array_keys( $checksums ) as $file ) {
		if ( str_starts_with( $file, 'wp-content' ) ) {
			continue;
		}
		if ( ! file_exists( ABSPATH . $file ) ) {
			continue;
		}
		if ( ! is_writable( ABSPATH . $file ) ) {
			$unwritable_files[] = $file;
		}
	}

	if ( $unwritable_files ) {
		if ( count( $unwritable_files ) > 20 ) {
			$unwritable_files   = array_slice( $unwritable_files, 0, 20 );
			$unwritable_files[] = '...';
		}
		return array(
			'description' => __( 'Some files are not writable by WordPress:' ) . ' <ul><li>' . implode( '</li><li>', $unwritable_files ) . '</li></ul>',
			'severity'    => 'fail',
		);
	} else {
		return array(
			'description' => __( 'All of your WordPress files are writable.' ),
			'severity'    => 'pass',
		);
	}
}