PluginProbe
The Innovative Form Builder – IvyForms / 1.4
The Innovative Form Builder – IvyForms v1.4
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Repository / Permissions / AccessRulesRepository.php

AccessRulesRepository.php in The Innovative Form Builder – IvyForms 1.4, at backend/src/Repository/Permissions/AccessRulesRepository.php

69 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights reserved.
5 */
6
7 namespace IvyForms\Repository\Permissions;
8
9 /**
10 * Persists IvyForms access rules (global form permissions) in a dedicated option.
11 */
12 class AccessRulesRepository
13 {
14 private const OPTION_KEY = 'ivyforms_access_rules';
15
16 /**
17 * @return array{version:int, rules:list<array<string,mixed>>}
18 */
19 public function getPayload(): array
20 {
21 $raw = get_option(self::OPTION_KEY, null);
22 if (!is_array($raw) || !isset($raw['rules']) || !is_array($raw['rules'])) {
23 return [
24 'version' => 1,
25 'rules' => [],
26 ];
27 }
28
29 $version = isset($raw['version']) ? (int) $raw['version'] : 1;
30
31 return [
32 'version' => max(1, $version),
33 'rules' => self::filterRuleEntries($raw['rules']),
34 ];
35 }
36
37 /**
38 * @param list<array<string,mixed>> $rules
39 */
40 public function saveRules(array $rules): void
41 {
42 $filtered = self::filterRuleEntries($rules);
43
44 $prev = get_option(self::OPTION_KEY, null);
45 $version = 1;
46 if (is_array($prev) && array_key_exists('version', $prev)) {
47 $version = max(1, (int) $prev['version']);
48 }
49
50 update_option(
51 self::OPTION_KEY,
52 [
53 'version' => $version,
54 'rules' => array_values($filtered),
55 ],
56 false
57 );
58 }
59
60 /**
61 * @param mixed[] $rules
62 * @return list<array<string,mixed>>
63 */
64 private static function filterRuleEntries(array $rules): array
65 {
66 return array_values(array_filter($rules, 'is_array'));
67 }
68 }
69