$new The new path data to be added or updated. */ public function update(array $new): bool { $paths = (array) get_option(self::OPTION_NAME, []); if ($new === [] || $new['path'] === '' || $new['type'] === '') { return false; } if ($new['type'] !== 'single' && $new['type'] !== 'group') { return false; } $paths[] = [ 'path' => sanitize_text_field(self::sanitizePath($new['path'])), 'type' => sanitize_text_field($new['type']), ]; return update_option(self::OPTION_NAME, array_values($paths)); } /** * Retrieve the custom paths. * * @return array> An array of paths with their types. */ public function get(): array { $data = get_option(self::OPTION_NAME, []); if (! is_array($data)) { $data = []; } return $data; } public function remove(int $index): bool { $paths = $this->get(); if (isset($paths[$index]) && ! empty($paths[$index])) { unset($paths[$index]); } if (count($paths) === 0) { return delete_option('kinsta-cache-additional-paths'); } return update_option('kinsta-cache-additional-paths', array_values($paths)); } private static function sanitizePath(string $input): string { $path = trim($input); $path = preg_replace('/^(\.\/+|\.\.\/+)/', '', $path); $path = preg_replace('/^\/+|\/+$/', '', $path); $path = preg_replace('/\/{2,}/', '/', $path); if (substr($input, -1) === '/' && $path !== '') { $path .= '/'; } return $path; } }