Automattic\WooCommerce\Internal\ProductFeed\Storage
JsonFileFeed::ensure_feed_dir_file_access
Upgrades a legacy deny from all .htaccess in an existing feed directory to allow file access.
Installs created before file access was enabled have a deny from all .htaccess here, which blocks feed downloads. This upgrades only that known legacy directive, in place. Anything else — an already-correct directive, custom rules a site or host added, a file we cannot read, or a missing file — is left untouched. (The directory's initial .htaccess is written when the directory is first created, by mkdir_p_not_indexable().)
Native file functions are used here (like the feed writes elsewhere in this class) rather than WP_Filesystem: the directory is local, and routing through a possibly FTP/SSH-backed filesystem could fail to initialize and leave the old deny from all in place even though the feed file itself was written natively. A failure is ignored (and logged) so it can never interrupt feed generation.
Method of the class: JsonFileFeed{}
No Hooks.
Returns
void. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->ensure_feed_dir_file_access( $directory_path ): void;
- $directory_path(string) (required)
- The feed directory path (trailing-slashed).
JsonFileFeed::ensure_feed_dir_file_access() JsonFileFeed::ensure feed dir file access code WC 11.1.2
private function ensure_feed_dir_file_access( string $directory_path ): void {
$htaccess_path = $directory_path . '.htaccess';
// Only act on an existing file. A missing .htaccess does not block downloads, so there is
// nothing to fix — and we should not create a file the directory did not already have.
if ( ! is_file( $htaccess_path ) ) {
return;
}
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$current_content = @file_get_contents( $htaccess_path );
// Upgrade only the known legacy `deny from all` directive. Leave anything else — already
// correct, custom rules, or a file we cannot read — untouched, never clobbering content
// we did not write.
if ( false === $current_content || FilesystemUtil::HTACCESS_DENY_ALL !== trim( $current_content ) ) {
return;
}
// Best effort: a failure must never interrupt feed generation, but log it — otherwise the
// feed would silently stay 403 behind the stale rule.
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
if ( false === @file_put_contents( $htaccess_path, FilesystemUtil::HTACCESS_ALLOW_FILE_ACCESS ) ) {
wc_get_logger()->warning(
'Could not update the product feed .htaccess to allow file access; generated feeds may remain inaccessible.',
array(
'source' => 'product-feed',
'path' => $htaccess_path,
)
);
}
}