false, 'owned' => $owned, 'reason' => 'not_writable', ); } if ( ! $owned && file_exists( $path ) ) { // Something else (host, another plugin, a manual upload) already // put a file here — never clobber it silently. return array( 'written' => false, 'owned' => false, 'reason' => 'conflict', ); } $bytes = @file_put_contents( $path, $content, LOCK_EX ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- filesystem outcome is checked via the return value, not surfaced as a PHP warning. if ( false === $bytes ) { return array( 'written' => false, 'owned' => $owned, 'reason' => 'write_failed', ); } return array( 'written' => true, 'owned' => true, 'reason' => 'ok', ); } /** * Delete the physical file, but only if the plugin owns it. Never removes * a foreign file. * * @param string $dir Absolute directory, trailing slash optional. * @param string $filename e.g. 'llms.txt'. * @param bool $owned Whether the plugin owns this filename. * @return bool True if deleted or already absent; false if refused or failed. */ public static function delete_if_owned( $dir, $filename, $owned ) { if ( ! $owned ) { return false; } $path = self::normalize_dir( $dir ) . $filename; if ( ! file_exists( $path ) ) { return true; } return (bool) @unlink( $path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- filesystem outcome is checked via the return value. } /** * Normalize a directory path to have exactly one trailing slash. * * @param string $dir Directory path. * @return string */ private static function normalize_dir( $dir ) { return rtrim( (string) $dir, '/\\' ) . '/'; } }