options-handler.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Admin\Tabs_Handlers; |
| 4 | |
| 5 | if ( ! defined( 'WPINC' ) ) { |
| 6 | die; |
| 7 | } |
| 8 | |
| 9 | class Options_Handler extends Base_Handler { |
| 10 | |
| 11 | const OPTIONS = array( |
| 12 | 'enable_dev_mode' => false, |
| 13 | 'clear_on_uninstall' => false, |
| 14 | 'disable_next_button' => true, |
| 15 | 'scroll_on_next' => false, |
| 16 | 'auto_focus' => false, |
| 17 | 'form_records_access_capability' => 'manage_options', |
| 18 | 'gfb_request_args_key' => '', |
| 19 | 'gfb_request_args_value' => '', |
| 20 | 'ssr_validation_method' => 'rest', |
| 21 | ); |
| 22 | |
| 23 | public function slug() { |
| 24 | return 'options-tab'; |
| 25 | } |
| 26 | |
| 27 | public function on_get_request() { |
| 28 | $options = array(); |
| 29 | |
| 30 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 31 | foreach ( self::OPTIONS as $name => $default ) { |
| 32 | |
| 33 | if ( ! array_key_exists( $name, $_POST ) ) { |
| 34 | continue; |
| 35 | } |
| 36 | if ( is_bool( $default ) ) { |
| 37 | $options[ $name ] = filter_var( |
| 38 | sanitize_key( $_POST[ $name ] ), |
| 39 | defined( 'FILTER_VALIDATE_BOOL' ) ? FILTER_VALIDATE_BOOL : FILTER_VALIDATE_BOOLEAN |
| 40 | ); |
| 41 | } else { |
| 42 | $options[ $name ] = sanitize_text_field( wp_unslash( $_POST[ $name ] ?? '' ) ); |
| 43 | } |
| 44 | } |
| 45 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 46 | $result = $this->update_options( $options ); |
| 47 | |
| 48 | $this->send_response( $result ); |
| 49 | } |
| 50 | |
| 51 | public function on_load() { |
| 52 | $this->set_jfb_request_args(); |
| 53 | return $this->get_options(); |
| 54 | } |
| 55 | |
| 56 | public function set_jfb_request_args() { |
| 57 | $options = $this->get_options(); |
| 58 | $update_data = array(); |
| 59 | |
| 60 | if ( empty( $options['gfb_request_args_key'] ) || ctype_digit( (string) $options['gfb_request_args_key'] ) ) { |
| 61 | $update_data['gfb_request_args_key'] = $this->jfb_generate_str(); |
| 62 | } |
| 63 | |
| 64 | if ( empty( $options['gfb_request_args_value'] ) || ctype_digit( (string) $options['gfb_request_args_value'] ) ) { |
| 65 | $update_data['gfb_request_args_value'] = $this->jfb_generate_str() . $this->jfb_generate_str(); |
| 66 | } |
| 67 | |
| 68 | if ( ! empty( $update_data ) ) { |
| 69 | $this->update_options( $update_data ); |
| 70 | $options = array_merge( $options, $update_data ); |
| 71 | } |
| 72 | |
| 73 | return $options; |
| 74 | } |
| 75 | |
| 76 | public function jfb_generate_str() { |
| 77 | $letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
| 78 | $digits = '0123456789'; |
| 79 | |
| 80 | $l = ''; |
| 81 | for ( $i = 0; $i < 3; $i++ ) { |
| 82 | $l .= $letters[ wp_rand( 0, strlen( $letters ) - 1 ) ]; |
| 83 | } |
| 84 | |
| 85 | $d = ''; |
| 86 | for ( $i = 0; $i < 3; $i++ ) { |
| 87 | $d .= $digits[ wp_rand( 0, strlen( $digits ) - 1 ) ]; |
| 88 | } |
| 89 | |
| 90 | $key_arr = str_split( $l . $d ); |
| 91 | shuffle( $key_arr ); |
| 92 | |
| 93 | return implode( '', $key_arr ); |
| 94 | } |
| 95 | } |
| 96 |