PluginProbe
The Innovative Form Builder – IvyForms / 1.5
The Innovative Form Builder – IvyForms v1.5
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 / Services / Permissions / AccessRuleMatcher.php

AccessRuleMatcher.php in The Innovative Form Builder – IvyForms 1.5, at backend/src/Services/Permissions/AccessRuleMatcher.php

112 lines 3.0 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\Services\Permissions;
8
9 // phpcs:disable PSR1.Files.SideEffects
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 /**
15 * Form-scope and principal matching helpers for stored access rules.
16 */
17 final class AccessRuleMatcher
18 {
19 /**
20 * @param mixed[] $rule
21 */
22 public static function appliesToForm(array $rule, ?int $formId): bool
23 {
24 $formIds = $rule['form_ids'] ?? null;
25 if ($formIds === null) {
26 return true;
27 }
28 if (!is_array($formIds)) {
29 return false;
30 }
31 if ($formId === null) {
32 return false;
33 }
34
35 return in_array($formId, array_map('intval', $formIds), true);
36 }
37
38 /**
39 * @param mixed[] $rule
40 */
41 public static function matchesUser(array $rule, \WP_User $user): bool
42 {
43 $id = isset($rule['user_id']) ? (int) $rule['user_id'] : 0;
44
45 return $id > 0 && $id === (int) $user->ID;
46 }
47
48 /**
49 * @param mixed[] $rule
50 */
51 public static function matchesRole(array $rule, \WP_User $user): bool
52 {
53 $slug = isset($rule['role_slug']) ? strtolower(trim((string) $rule['role_slug'])) : '';
54 if ($slug === '') {
55 return false;
56 }
57
58 return in_array($slug, self::getUserRoleSlugs($user), true);
59 }
60
61 /**
62 * @param list<array<string,mixed>> $rules
63 * @return list<array<string,mixed>>
64 */
65 public static function sortForMerge(array $rules): array
66 {
67 usort(
68 $rules,
69 static function (array $leftRule, array $rightRule): int {
70 $leftUpdatedAt = isset($leftRule['updated_at']) ? (int) $leftRule['updated_at'] : 0;
71 $rightUpdatedAt = isset($rightRule['updated_at']) ? (int) $rightRule['updated_at'] : 0;
72 if ($leftUpdatedAt !== $rightUpdatedAt) {
73 return $leftUpdatedAt <=> $rightUpdatedAt;
74 }
75 $leftCreatedAt = isset($leftRule['created_at']) ? (int) $leftRule['created_at'] : 0;
76 $rightCreatedAt = isset($rightRule['created_at']) ? (int) $rightRule['created_at'] : 0;
77
78 return $leftCreatedAt <=> $rightCreatedAt;
79 }
80 );
81
82 return $rules;
83 }
84
85 /**
86 * @return list<string>
87 */
88 private static function getUserRoleSlugs(\WP_User $user): array
89 {
90 $roleSet = [];
91
92 foreach ((array) $user->roles as $role) {
93 if (!is_string($role) || $role === '') {
94 continue;
95 }
96 $roleSet[strtolower($role)] = true;
97 }
98
99 foreach ((array) $user->caps as $cap => $enabled) {
100 if (!$enabled || !is_string($cap) || $cap === '') {
101 continue;
102 }
103 if (!wp_roles()->is_role($cap)) {
104 continue;
105 }
106 $roleSet[strtolower($cap)] = true;
107 }
108
109 return array_keys($roleSet);
110 }
111 }
112