| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | */ |
| 14 | 14 | class FrmFormState { |
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | - * @var FrmFormState | |
| 17 | + * @var FrmFormState $instance | |
| 18 | 18 | */ |
| 19 | 19 | private static $instance; |
| 20 | 20 | |
| 21 | 21 | /** |
| @@ -45,9 +45,9 @@ | ||
| 45 | 45 | /** |
| 46 | 46 | * @return bool true if just initialized. |
| 47 | 47 | */ |
| 48 | 48 | private static function maybe_initialize() { |
| 49 | - if ( empty( self::$instance ) ) { | |
| 49 | + if ( ! isset( self::$instance ) ) { | |
| 50 | 50 | self::$instance = new self(); |
| 51 | 51 | return true; |
| 52 | 52 | } |
| 53 | 53 | return false; |
| @@ -74,9 +74,12 @@ | ||
| 74 | 74 | return self::$instance->get( $key, $default ); |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | public function get( $key, $default ) { |
| 78 | - return $this->state[ $key ] ?? $default; | |
| 78 | + if ( isset( $this->state[ $key ] ) ) { | |
| 79 | + return $this->state[ $key ]; | |
| 80 | + } | |
| 81 | + return $default; | |
| 79 | 82 | } |
| 80 | 83 | |
| 81 | 84 | /** |
| 82 | 85 | * Render a basic version of the state field from Pro. |
| @@ -92,16 +95,14 @@ | ||
| 92 | 95 | // This way we can also avoid duplicate state fields if Pro isn't up to date. |
| 93 | 96 | return; |
| 94 | 97 | } |
| 95 | 98 | |
| 96 | - if ( empty( self::$instance ) && ! self::get_state_from_request() ) { | |
| 99 | + if ( empty( $form->options['ajax_submit'] ) ) { | |
| 100 | + // This is only required for AJAX submit. | |
| 97 | 101 | return; |
| 98 | 102 | } |
| 99 | 103 | |
| 100 | - $honeypot_field_id = self::$instance->get( 'honeypot_field_id', 0 ); | |
| 101 | - | |
| 102 | - if ( empty( $form->options['ajax_submit'] ) && ! $honeypot_field_id ) { | |
| 103 | - // This is only required for AJAX submit, or when the honeypot field is on the page. | |
| 104 | + if ( empty( self::$instance ) && ! self::get_state_from_request() ) { | |
| 104 | 105 | return; |
| 105 | 106 | } |
| 106 | 107 | |
| 107 | 108 | $state_title = ! empty( self::$instance->state['title'] ) ? 1 : 0; |
| @@ -108,10 +109,10 @@ | ||
| 108 | 109 | $state_description = ! empty( self::$instance->state['description'] ) ? 1 : 0; |
| 109 | 110 | $settings_title = ! empty( $form->options['show_title'] ) ? 1 : 0; |
| 110 | 111 | $settings_description = ! empty( $form->options['show_description'] ) ? 1 : 0; |
| 111 | 112 | |
| 112 | - if ( $state_title === $settings_title && $state_description === $settings_description && ! $honeypot_field_id ) { | |
| 113 | - // Avoid state field if it matches form settings and there is no honeypot. | |
| 113 | + if ( $state_title === $settings_title && $state_description === $settings_description ) { | |
| 114 | + // Avoid state field if it matches form settings. | |
| 114 | 115 | return; |
| 115 | 116 | } |
| 116 | 117 | |
| 117 | 118 | self::$instance->render_state_field(); |
| @@ -143,11 +144,8 @@ | ||
| 143 | 144 | /** |
| 144 | 145 | * @return void |
| 145 | 146 | */ |
| 146 | 147 | public function render_state_field() { |
| 147 | - if ( ! self::open_ssl_is_installed() ) { | |
| 148 | - return; | |
| 149 | - } | |
| 150 | 148 | if ( ! $this->state && ! self::get_state_from_request() ) { |
| 151 | 149 | return; |
| 152 | 150 | } |
| 153 | 151 | $state_string = $this->get_state_string(); |
| @@ -157,11 +155,8 @@ | ||
| 157 | 155 | /** |
| 158 | 156 | * @return string |
| 159 | 157 | */ |
| 160 | 158 | private function get_state_string() { |
| 161 | - if ( ! self::open_ssl_is_installed() ) { | |
| 162 | - return ''; | |
| 163 | - } | |
| 164 | 159 | $secret = self::get_encryption_secret(); |
| 165 | 160 | $compressed_state = $this->compressed_state(); |
| 166 | 161 | $json_encoded = json_encode( $compressed_state ); |
| 167 | 162 | $encrypted = openssl_encrypt( $json_encoded, 'AES-128-ECB', $secret ); |
| @@ -168,18 +163,8 @@ | ||
| 168 | 163 | return $encrypted; |
| 169 | 164 | } |
| 170 | 165 | |
| 171 | 166 | /** |
| 172 | - * Returns true if open SSL is installed. | |
| 173 | - * | |
| 174 | - * @since 6.12 | |
| 175 | - * @return bool | |
| 176 | - */ | |
| 177 | - private static function open_ssl_is_installed() { | |
| 178 | - return function_exists( 'openssl_encrypt' ); | |
| 179 | - } | |
| 180 | - | |
| 181 | - /** | |
| 182 | 167 | * Return state but with shorter keys to use for the state string. |
| 183 | 168 | * |
| 184 | 169 | * @return array |
| 185 | 170 | */ |
| @@ -216,10 +201,8 @@ | ||
| 216 | 201 | case 'd': |
| 217 | 202 | return 'description'; |
| 218 | 203 | case 't': |
| 219 | 204 | return 'title'; |
| 220 | - case 'h': | |
| 221 | - return 'honeypot_field_id'; | |
| 222 | 205 | } |
| 223 | 206 | return $key; |
| 224 | 207 | } |
| 225 | 208 | |
| @@ -235,9 +218,9 @@ | ||
| 235 | 218 | } |
| 236 | 219 | |
| 237 | 220 | // We don't have a secret, so let's generate one. |
| 238 | 221 | $secret_key = is_callable( 'sodium_crypto_secretbox_keygen' ) ? sodium_crypto_secretbox_keygen() : wp_generate_password( 32, true, true ); |
| 239 | - update_option( 'frm_form_state_key', base64_encode( $secret_key ), 'no' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | |
| 222 | + add_option( 'frm_form_state_key', base64_encode( $secret_key ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | |
| 240 | 223 | |
| 241 | 224 | return $secret_key; |
| 242 | 225 | } |
| 243 | 226 | } |