WP_Site_Health_Auto_Updates::test_vcs_abspath(): array

In this article

Checks if WordPress is controlled by a VCS (Git, Subversion etc).

Return

array The test results.

Source

public function test_vcs_abspath() {
	$context_dirs = array( ABSPATH );
	$vcs_dirs     = array( '.svn', '.git', '.hg', '.bzr' );
	$check_dirs   = array();

	foreach ( $context_dirs as $context_dir ) {
		// Walk up from $context_dir to the root.
		do {
			$check_dirs[] = $context_dir;

			// Once we've hit '/' or 'C:\', we need to stop. dirname will keep returning the input here.
			if ( dirname( $context_dir ) === $context_dir ) {
				break;
			}

			// Continue one level at a time.
		} while ( $context_dir = dirname( $context_dir ) );
	}

	$check_dirs = array_unique( $check_dirs );

	// Search all directories we've found for evidence of version control.
	foreach ( $vcs_dirs as $vcs_dir ) {
		foreach ( $check_dirs as $check_dir ) {
			// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition,Squiz.PHP.DisallowMultipleAssignments
			if ( $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" ) ) {
				break 2;
			}
		}
	}

	/** This filter is documented in wp-admin/includes/class-wp-automatic-updater.php */
	if ( $checkout && ! apply_filters( 'automatic_updates_is_vcs_checkout', true, ABSPATH ) ) {
		return array(
			'description' => sprintf(
				/* translators: 1: Folder name. 2: Version control directory. 3: Filter name. */
				__( 'The folder %1$s was detected as being under version control (%2$s), but the %3$s filter is allowing updates.' ),
				'<code>' . $check_dir . '</code>',
				"<code>$vcs_dir</code>",
				'<code>automatic_updates_is_vcs_checkout</code>'
			),
			'severity'    => 'info',
		);
	}

	if ( $checkout ) {
		return array(
			'description' => sprintf(
				/* translators: 1: Folder name. 2: Version control directory. */
				__( 'The folder %1$s was detected as being under version control (%2$s).' ),
				'<code>' . $check_dir . '</code>',
				"<code>$vcs_dir</code>"
			),
			'severity'    => 'warning',
		);
	}

	return array(
		'description' => __( 'No version control systems were detected.' ),
		'severity'    => 'pass',
	);
}

Hooks

apply_filters( ‘automatic_updates_is_vcs_checkout’, bool $checkout, string $context )

Filters whether the automatic updater should consider a filesystem location to be potentially managed by a version control system.

Changelog

VersionDescription
5.2.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.