Automattic\WooCommerce\Internal\Api

QueryCache::ensure_opcache_dir_writableprivateWC 1.0

Ensure the OPcache cache directory exists and is writable.

Creates the directory on first use and drops a deny-all .htaccess and an empty index.html alongside it. Returns false if creation fails or the directory ends up non-writable.

Method of the class: QueryCache{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->ensure_opcache_dir_writable(): bool;

QueryCache::ensure_opcache_dir_writable() code WC 10.9.1

private function ensure_opcache_dir_writable(): bool {
	$dir = self::get_opcache_cache_dir();

	if ( '' === $dir ) {
		return false;
	}

	if ( ! is_dir( $dir ) && ! wp_mkdir_p( $dir ) ) {
		return false;
	}

	$fs = ResolverHelpers::wp_filesystem();
	if ( ! $fs || ! $fs->is_writable( $dir ) ) {
		return false;
	}

	// Best-effort hardening; ignore failures (e.g. read-only permissions).
	$htaccess = $dir . '/.htaccess';
	$index    = $dir . '/index.html';
	if ( ! file_exists( $htaccess ) ) {
		$fs->put_contents( $htaccess, "Deny from all\n" );
	}
	if ( ! file_exists( $index ) ) {
		$fs->put_contents( $index, '' );
	}

	return true;
}