attempted ) { return $this->fs; } $this->attempted = true; global $wp_filesystem; if ( ! function_exists( 'WP_Filesystem' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } // Only the 'direct' transport runs without user-supplied credentials. if ( 'direct' !== get_filesystem_method() ) { return null; } if ( ! WP_Filesystem() ) { return null; } if ( ! $wp_filesystem instanceof \WP_Filesystem_Base ) { return null; } $this->fs = $wp_filesystem; return $this->fs; } /** * Returns the site's document root, or an empty string when it cannot be * trusted. * * @return string Trailing-slashed absolute path, or '' when undeterminable. */ public function home_path(): string { if ( ! function_exists( 'get_home_path' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $path = (string) get_home_path(); if ( '' === $path || '/' === $path || ! is_dir( $path ) ) { return ''; } return trailingslashit( $path ); } /** * Returns the message shown when the document root cannot be located. * * @return string Human-readable explanation. */ public function get_unknown_home_message(): string { return __( 'WordPress could not work out which folder your site is served from, so this file cannot be found or written safely. This usually means the WordPress Address and Site Address settings do not match how the server actually reaches your site. Nothing has been changed.', 'ai-blackout' ); } /** * Whether the filesystem is usable. * * @return bool True when reads and writes can be attempted. */ public function is_available(): bool { return null !== $this->get(); } /** * Returns the message explaining why writing is unavailable. * * @return string Human-readable explanation. */ public function get_unavailable_message(): string { return __( 'WordPress cannot write files directly on this host, so this file cannot be updated automatically. Copy the block below and add it to the file yourself.', 'ai-blackout' ); } /** * Whether a path exists. * * @param string $path Absolute path. * @return bool True when it exists. */ public function exists( string $path ): bool { $fs = $this->get(); return null !== $fs && $fs->exists( $path ); } /** * Whether a path is writable. * * @param string $path Absolute path. * @return bool True when writable. */ public function is_writable( string $path ): bool { $fs = $this->get(); return null !== $fs && $fs->is_writable( $path ); } /** * Reads a file. * * @param string $path Absolute path. * @return string|null Contents, or null on failure. */ public function read( string $path ): ?string { $fs = $this->get(); if ( null === $fs || ! $fs->exists( $path ) ) { return null; } $contents = $fs->get_contents( $path ); return false === $contents ? null : $contents; } /** * Writes a file. * * @param string $path Absolute path. * @param string $contents Contents to write. * @return bool True on success. */ public function write( string $path, string $contents ): bool { $fs = $this->get(); if ( null === $fs ) { return false; } return $fs->put_contents( $path, $contents, FS_CHMOD_FILE ); } /** * Deletes a file. * * @param string $path Absolute path. * @return bool True when the file is gone afterwards. */ public function delete( string $path ): bool { $fs = $this->get(); if ( null === $fs ) { return false; } if ( ! $fs->exists( $path ) ) { return true; } return $fs->delete( $path ); } }