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 ); } } /** * Wrapper to initialize the WP filesystem with defined credentials if they are available. * * @return bool True if the $wp_filesystem global was successfully initialized. */ protected static function initialize_wp_filesystem(): bool { global $wp_filesystem; if ( $wp_filesystem instanceof WP_Filesystem_Base ) { return true; } require_once ABSPATH . 'wp-admin/includes/file.php'; $method = get_filesystem_method(); $initialized = false; if ( 'direct' === $method ) { $initialized = WP_Filesystem(); } elseif ( false !== $method ) { // See https://core.trac.wordpress.org/changeset/56341. ob_start(); $credentials = request_filesystem_credentials( '' ); ob_end_clean(); $initialized = $credentials && WP_Filesystem( $credentials ); } return is_null( $initialized ) ? false : $initialized; } }