Automattic\WooCommerce\Internal\Utilities

FilesystemUtil::mkdir_p_not_indexable()public staticWC 9.3.0

Recursively creates a directory (if it doesn't exist) and adds an empty index.html and a .htaccess to prevent directory listing.

Method of the class: FilesystemUtil{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = FilesystemUtil::mkdir_p_not_indexable( $path ): void;
$path(string) (required)
Directory to create.

Changelog

Since 9.3.0 Introduced.

FilesystemUtil::mkdir_p_not_indexable() code WC 9.3.3

public static function mkdir_p_not_indexable( string $path ): void {
	$wp_fs = self::get_wp_filesystem();

	if ( $wp_fs->is_dir( $path ) ) {
		return;
	}

	if ( ! wp_mkdir_p( $path ) ) {
		throw new \Exception( esc_html( sprintf( 'Could not create directory: %s.', wp_basename( $path ) ) ) );
	}

	$files = array(
		'.htaccess'  => 'deny from all',
		'index.html' => '',
	);

	foreach ( $files as $name => $content ) {
		$wp_fs->put_contents( trailingslashit( $path ) . $name, $content );
	}
}