| @@ -5,8 +5,10 @@ | ||
| 5 | 5 | if ( ! defined( 'WPINC' ) ) { |
| 6 | 6 | die; |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | +use Jet_Form_Builder\Classes\Tools; | |
| 10 | + | |
| 9 | 11 | class Options_Handler extends Base_Handler { |
| 10 | 12 | |
| 11 | 13 | const OPTIONS = array( |
| 12 | 14 | 'enable_dev_mode' => false, |
| @@ -17,8 +19,9 @@ | ||
| 17 | 19 | 'form_records_access_capability' => 'manage_options', |
| 18 | 20 | 'gfb_request_args_key' => '', |
| 19 | 21 | 'gfb_request_args_value' => '', |
| 20 | 22 | 'ssr_validation_method' => 'rest', |
| 23 | + 'self_promotable_roles' => array(), | |
| 21 | 24 | ); |
| 22 | 25 | |
| 23 | 26 | public function slug() { |
| 24 | 27 | return 'options-tab'; |
| @@ -25,8 +28,9 @@ | ||
| 25 | 28 | } |
| 26 | 29 | |
| 27 | 30 | public function on_get_request() { |
| 28 | 31 | $options = array(); |
| 32 | + $prev_options = $this->get_options(); | |
| 29 | 33 | |
| 30 | 34 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 31 | 35 | foreach ( self::OPTIONS as $name => $default ) { |
| 32 | 36 | |
| @@ -37,8 +41,21 @@ | ||
| 37 | 41 | $options[ $name ] = filter_var( |
| 38 | 42 | sanitize_key( $_POST[ $name ] ), |
| 39 | 43 | defined( 'FILTER_VALIDATE_BOOL' ) ? FILTER_VALIDATE_BOOL : FILTER_VALIDATE_BOOLEAN |
| 40 | 44 | ); |
| 45 | + } elseif ( is_array( $default ) ) { | |
| 46 | + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized below with sanitize_key for each submitted role. | |
| 47 | + $raw_roles = wp_unslash( $_POST[ $name ] ); | |
| 48 | + $roles = is_array( $raw_roles ) ? $raw_roles : array(); | |
| 49 | + $roles = array_map( 'sanitize_key', $roles ); | |
| 50 | + $roles = array_filter( $roles ); | |
| 51 | + $roles = array_values( array_unique( $roles ) ); | |
| 52 | + | |
| 53 | + $registered_roles = array_keys( Tools::get_registered_roles_safe() ); | |
| 54 | + $roles = array_values( array_intersect( $roles, $registered_roles ) ); | |
| 55 | + $roles = array_values( array_diff( $roles, array( 'administrator' ) ) ); | |
| 56 | + | |
| 57 | + $options[ $name ] = $roles; | |
| 41 | 58 | } else { |
| 42 | 59 | $options[ $name ] = sanitize_text_field( wp_unslash( $_POST[ $name ] ?? '' ) ); |
| 43 | 60 | } |
| 44 | 61 | } |
| @@ -44,14 +61,35 @@ | ||
| 44 | 61 | } |
| 45 | 62 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 46 | 63 | $result = $this->update_options( $options ); |
| 47 | 64 | |
| 65 | + if ( | |
| 66 | + array_key_exists( 'self_promotable_roles', $options ) && | |
| 67 | + ( $prev_options['self_promotable_roles'] ?? array() ) !== $options['self_promotable_roles'] | |
| 68 | + ) { | |
| 69 | + do_action( 'jet-form-builder/update-user/invalidate-role-scan' ); | |
| 70 | + } | |
| 71 | + | |
| 48 | 72 | $this->send_response( $result ); |
| 49 | 73 | } |
| 50 | 74 | |
| 51 | 75 | public function on_load() { |
| 52 | 76 | $this->set_jfb_request_args(); |
| 53 | - return $this->get_options(); | |
| 77 | + $options = $this->get_options(); | |
| 78 | + $options['available_roles'] = array(); | |
| 79 | + | |
| 80 | + foreach ( Tools::get_registered_roles_safe() as $role => $data ) { | |
| 81 | + if ( 'administrator' === $role ) { | |
| 82 | + continue; | |
| 83 | + } | |
| 84 | + | |
| 85 | + $options['available_roles'][] = array( | |
| 86 | + 'value' => $role, | |
| 87 | + 'label' => $data['name'] ?? $role, | |
| 88 | + ); | |
| 89 | + } | |
| 90 | + | |
| 91 | + return $options; | |
| 54 | 92 | } |
| 55 | 93 | |
| 56 | 94 | public function set_jfb_request_args() { |
| 57 | 95 | $options = $this->get_options(); |