description-property.php
2 years ago
display-name-property.php
2 years ago
first-name-property.php
2 years ago
last-name-property.php
2 years ago
nick-name-property.php
2 years ago
update-action.php
2 years ago
user-compare-password-property.php
7 months ago
user-confirm-password-property.php
2 years ago
user-email-property.php
2 years ago
user-id-property.php
2 years ago
user-meta-property.php
2 years ago
user-modifier.php
2 months ago
user-nicename-property.php
2 years ago
user-password-property.php
2 years ago
user-role-property.php
2 months ago
user-url-property.php
2 years ago
user-role-property.php
187 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Actions_V2\Update_User\Properties; |
| 5 | |
| 6 | use Jet_Form_Builder\Actions\Methods\Abstract_Modifier; |
| 7 | use Jet_Form_Builder\Actions\Methods\Base_Object_Property; |
| 8 | use Jet_Form_Builder\Classes\Arrayable\Array_Continue_Exception; |
| 9 | use Jet_Form_Builder\Classes\Tools; |
| 10 | use Jet_Form_Builder\Exceptions\Silence_Exception; |
| 11 | |
| 12 | // If this file is called directly, abort. |
| 13 | if ( ! defined( 'WPINC' ) ) { |
| 14 | die; |
| 15 | } |
| 16 | |
| 17 | class User_Role_Property extends Base_Object_Property { |
| 18 | |
| 19 | public function get_id(): string { |
| 20 | return 'role'; |
| 21 | } |
| 22 | |
| 23 | public function can_attach( string $key, $value ): bool { |
| 24 | if ( empty( $value ) || 'administrator' === $value ) { |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | return parent::can_attach( $key, $value ); |
| 29 | } |
| 30 | |
| 31 | public function do_before( string $key, $value, Abstract_Modifier $modifier ) { |
| 32 | $roles = $this->normalize_roles( $value ); |
| 33 | |
| 34 | if ( Tools::is_webhook() || current_user_can( 'promote_users' ) ) { |
| 35 | parent::do_before( $key, $roles, $modifier ); |
| 36 | |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | if ( $this->is_noop_role_update( $modifier, $roles ) ) { |
| 41 | parent::do_before( $key, $roles, $modifier ); |
| 42 | |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | if ( ! $this->can_self_promote_to_roles( $modifier, $roles ) ) { |
| 47 | $this->exclude(); |
| 48 | |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | parent::do_before( $key, $roles, $modifier ); |
| 53 | } |
| 54 | |
| 55 | public function get_related(): array { |
| 56 | return array( 'ID' ); |
| 57 | } |
| 58 | |
| 59 | public function do_after( Abstract_Modifier $modifier ) { |
| 60 | /** @var User_Id_Property $id */ |
| 61 | $id = $modifier->get( 'ID' ); |
| 62 | |
| 63 | if ( empty( $this->value ) ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | if ( ! is_array( $this->value ) ) { |
| 68 | $this->value = array( $this->value ); |
| 69 | } |
| 70 | |
| 71 | if ( ! Tools::is_webhook() ) { |
| 72 | $available_roles = current_user_can( 'promote_users' ) |
| 73 | ? Tools::get_editable_roles_safe() |
| 74 | : Tools::get_registered_roles_safe(); |
| 75 | |
| 76 | if ( empty( $available_roles ) ) { |
| 77 | $this->exclude(); |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | $this->value = array_values( |
| 83 | array_filter( |
| 84 | $this->value, |
| 85 | function ( string $role ) use ( $available_roles ): bool { |
| 86 | return 'administrator' !== $role && isset( $available_roles[ $role ] ); |
| 87 | } |
| 88 | ) |
| 89 | ); |
| 90 | |
| 91 | if ( empty( $this->value ) ) { |
| 92 | $this->exclude(); |
| 93 | |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | $main_role = array_shift( $this->value ); |
| 99 | |
| 100 | if ( 'administrator' !== $main_role ) { |
| 101 | $id->user->set_role( $main_role ); |
| 102 | } |
| 103 | |
| 104 | foreach ( $this->value as $role ) { |
| 105 | if ( 'administrator' !== $role ) { |
| 106 | $id->user->add_role( $role ); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * @return string |
| 113 | * @throws Array_Continue_Exception |
| 114 | */ |
| 115 | public function get_label(): string { |
| 116 | throw new Array_Continue_Exception(); |
| 117 | } |
| 118 | |
| 119 | public function get_value( Abstract_Modifier $modifier ) { |
| 120 | throw new Silence_Exception(); |
| 121 | } |
| 122 | |
| 123 | private function normalize_roles( $roles ): array { |
| 124 | if ( ! is_array( $roles ) ) { |
| 125 | $roles = array( $roles ); |
| 126 | } |
| 127 | |
| 128 | $roles = array_map( 'strval', $roles ); |
| 129 | $roles = array_filter( $roles ); |
| 130 | $roles = array_values( array_unique( $roles ) ); |
| 131 | |
| 132 | return $roles; |
| 133 | } |
| 134 | |
| 135 | private function is_noop_role_update( Abstract_Modifier $modifier, array $requested_roles ): bool { |
| 136 | if ( ! $modifier instanceof User_Modifier ) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /** @var User_Id_Property $id */ |
| 141 | $id = $modifier->get( 'ID' ); |
| 142 | |
| 143 | if ( ! is_a( $id->user ?? null, \WP_User::class ) ) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | return $this->normalize_roles( $id->user->roles ) |
| 148 | === $this->normalize_roles( $requested_roles ); |
| 149 | } |
| 150 | |
| 151 | private function can_self_promote_to_roles( Abstract_Modifier $modifier, array $roles ): bool { |
| 152 | if ( ! $modifier instanceof User_Modifier ) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | /** @var User_Id_Property $id */ |
| 157 | $id = $modifier->get( 'ID' ); |
| 158 | |
| 159 | if ( ! is_a( $id->user ?? null, \WP_User::class ) ) { |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | if ( get_current_user_id() !== (int) $id->user->ID ) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | $allowed_roles = $modifier->get_self_promotable_roles(); |
| 168 | $current_roles = $this->normalize_roles( $id->user->roles ); |
| 169 | |
| 170 | if ( empty( $allowed_roles ) && empty( $current_roles ) ) { |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | foreach ( $roles as $role ) { |
| 175 | if ( in_array( $role, $current_roles, true ) ) { |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | if ( ! in_array( $role, $allowed_roles, true ) ) { |
| 180 | return false; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | } |
| 187 |