options-handler.php
134 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Admin\Tabs_Handlers; |
| 4 | |
| 5 | if ( ! defined( 'WPINC' ) ) { |
| 6 | die; |
| 7 | } |
| 8 | |
| 9 | use Jet_Form_Builder\Classes\Tools; |
| 10 | |
| 11 | class Options_Handler extends Base_Handler { |
| 12 | |
| 13 | const OPTIONS = array( |
| 14 | 'enable_dev_mode' => false, |
| 15 | 'clear_on_uninstall' => false, |
| 16 | 'disable_next_button' => true, |
| 17 | 'scroll_on_next' => false, |
| 18 | 'auto_focus' => false, |
| 19 | 'form_records_access_capability' => 'manage_options', |
| 20 | 'gfb_request_args_key' => '', |
| 21 | 'gfb_request_args_value' => '', |
| 22 | 'ssr_validation_method' => 'rest', |
| 23 | 'self_promotable_roles' => array(), |
| 24 | ); |
| 25 | |
| 26 | public function slug() { |
| 27 | return 'options-tab'; |
| 28 | } |
| 29 | |
| 30 | public function on_get_request() { |
| 31 | $options = array(); |
| 32 | $prev_options = $this->get_options(); |
| 33 | |
| 34 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 35 | foreach ( self::OPTIONS as $name => $default ) { |
| 36 | |
| 37 | if ( ! array_key_exists( $name, $_POST ) ) { |
| 38 | continue; |
| 39 | } |
| 40 | if ( is_bool( $default ) ) { |
| 41 | $options[ $name ] = filter_var( |
| 42 | sanitize_key( $_POST[ $name ] ), |
| 43 | defined( 'FILTER_VALIDATE_BOOL' ) ? FILTER_VALIDATE_BOOL : FILTER_VALIDATE_BOOLEAN |
| 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; |
| 58 | } else { |
| 59 | $options[ $name ] = sanitize_text_field( wp_unslash( $_POST[ $name ] ?? '' ) ); |
| 60 | } |
| 61 | } |
| 62 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 63 | $result = $this->update_options( $options ); |
| 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 | |
| 72 | $this->send_response( $result ); |
| 73 | } |
| 74 | |
| 75 | public function on_load() { |
| 76 | $this->set_jfb_request_args(); |
| 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; |
| 92 | } |
| 93 | |
| 94 | public function set_jfb_request_args() { |
| 95 | $options = $this->get_options(); |
| 96 | $update_data = array(); |
| 97 | |
| 98 | if ( empty( $options['gfb_request_args_key'] ) || ctype_digit( (string) $options['gfb_request_args_key'] ) ) { |
| 99 | $update_data['gfb_request_args_key'] = $this->jfb_generate_str(); |
| 100 | } |
| 101 | |
| 102 | if ( empty( $options['gfb_request_args_value'] ) || ctype_digit( (string) $options['gfb_request_args_value'] ) ) { |
| 103 | $update_data['gfb_request_args_value'] = $this->jfb_generate_str() . $this->jfb_generate_str(); |
| 104 | } |
| 105 | |
| 106 | if ( ! empty( $update_data ) ) { |
| 107 | $this->update_options( $update_data ); |
| 108 | $options = array_merge( $options, $update_data ); |
| 109 | } |
| 110 | |
| 111 | return $options; |
| 112 | } |
| 113 | |
| 114 | public function jfb_generate_str() { |
| 115 | $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 116 | $digits = '0123456789'; |
| 117 | |
| 118 | $l = ''; |
| 119 | for ( $i = 0; $i < 3; $i++ ) { |
| 120 | $l .= $letters[ wp_rand( 0, strlen( $letters ) - 1 ) ]; |
| 121 | } |
| 122 | |
| 123 | $d = ''; |
| 124 | for ( $i = 0; $i < 3; $i++ ) { |
| 125 | $d .= $digits[ wp_rand( 0, strlen( $digits ) - 1 ) ]; |
| 126 | } |
| 127 | |
| 128 | $key_arr = str_split( $l . $d ); |
| 129 | shuffle( $key_arr ); |
| 130 | |
| 131 | return implode( '', $key_arr ); |
| 132 | } |
| 133 | } |
| 134 |