| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | */ |
| 14 | 14 | class FrmFormState { |
| 15 | 15 | |
| 16 | 16 | /** |
| 17 | - * @var FrmFormState|null | |
| 17 | + * @var FrmFormState | |
| 18 | 18 | */ |
| 19 | 19 | private static $instance; |
| 20 | 20 | |
| 21 | 21 | /** |
| @@ -29,9 +29,8 @@ | ||
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * @param string $key |
| 32 | 32 | * @param mixed $value |
| 33 | - * | |
| 34 | 33 | * @return void |
| 35 | 34 | */ |
| 36 | 35 | public static function set_initial_value( $key, $value ) { |
| 37 | 36 | if ( is_callable( 'FrmProFormState::set_initial_value' ) ) { |
| @@ -46,9 +45,9 @@ | ||
| 46 | 45 | /** |
| 47 | 46 | * @return bool true if just initialized. |
| 48 | 47 | */ |
| 49 | 48 | private static function maybe_initialize() { |
| 50 | - if ( ! self::$instance ) { | |
| 49 | + if ( empty( self::$instance ) ) { | |
| 51 | 50 | self::$instance = new self(); |
| 52 | 51 | return true; |
| 53 | 52 | } |
| 54 | 53 | return false; |
| @@ -56,9 +55,8 @@ | ||
| 56 | 55 | |
| 57 | 56 | /** |
| 58 | 57 | * @param string $key |
| 59 | 58 | * @param mixed $value |
| 60 | - * | |
| 61 | 59 | * @return void |
| 62 | 60 | */ |
| 63 | 61 | public function set( $key, $value ) { |
| 64 | 62 | $this->state[ $key ] = $value; |
| @@ -66,9 +64,8 @@ | ||
| 66 | 64 | |
| 67 | 65 | /** |
| 68 | 66 | * @param string $key |
| 69 | 67 | * @param mixed $default |
| 70 | - * | |
| 71 | 68 | * @return mixed |
| 72 | 69 | */ |
| 73 | 70 | public static function get_from_request( $key, $default ) { |
| 74 | 71 | if ( self::maybe_initialize() ) { |
| @@ -76,16 +73,13 @@ | ||
| 76 | 73 | } |
| 77 | 74 | return self::$instance->get( $key, $default ); |
| 78 | 75 | } |
| 79 | 76 | |
| 80 | - /** | |
| 81 | - * @param string $key | |
| 82 | - * @param mixed $default | |
| 83 | - * | |
| 84 | - * @return mixed | |
| 85 | - */ | |
| 86 | 77 | public function get( $key, $default ) { |
| 87 | - return $this->state[ $key ] ?? $default; | |
| 78 | + if ( isset( $this->state[ $key ] ) ) { | |
| 79 | + return $this->state[ $key ]; | |
| 80 | + } | |
| 81 | + return $default; | |
| 88 | 82 | } |
| 89 | 83 | |
| 90 | 84 | /** |
| 91 | 85 | * Render a basic version of the state field from Pro. |
| @@ -92,9 +86,8 @@ | ||
| 92 | 86 | * This is required only when submitting with AJAX. |
| 93 | 87 | * It is used to track the value of a title=1|0 or description=1|0 option in a [formidable] shortcode. |
| 94 | 88 | * |
| 95 | 89 | * @param stdClass $form |
| 96 | - * | |
| 97 | 90 | * @return void |
| 98 | 91 | */ |
| 99 | 92 | public static function maybe_render_state_field( $form ) { |
| 100 | 93 | if ( is_callable( 'FrmProFormState::maybe_render_state_field' ) ) { |
| @@ -102,16 +95,14 @@ | ||
| 102 | 95 | // This way we can also avoid duplicate state fields if Pro isn't up to date. |
| 103 | 96 | return; |
| 104 | 97 | } |
| 105 | 98 | |
| 106 | - if ( ! self::$instance && ! self::get_state_from_request() ) { | |
| 99 | + if ( empty( $form->options['ajax_submit'] ) ) { | |
| 100 | + // This is only required for AJAX submit. | |
| 107 | 101 | return; |
| 108 | 102 | } |
| 109 | 103 | |
| 110 | - $honeypot_field_id = self::$instance->get( 'honeypot_field_id', 0 ); | |
| 111 | - | |
| 112 | - if ( empty( $form->options['ajax_submit'] ) && ! $honeypot_field_id ) { | |
| 113 | - // 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() ) { | |
| 114 | 105 | return; |
| 115 | 106 | } |
| 116 | 107 | |
| 117 | 108 | $state_title = ! empty( self::$instance->state['title'] ) ? 1 : 0; |
| @@ -118,10 +109,10 @@ | ||
| 118 | 109 | $state_description = ! empty( self::$instance->state['description'] ) ? 1 : 0; |
| 119 | 110 | $settings_title = ! empty( $form->options['show_title'] ) ? 1 : 0; |
| 120 | 111 | $settings_description = ! empty( $form->options['show_description'] ) ? 1 : 0; |
| 121 | 112 | |
| 122 | - if ( $state_title === $settings_title && $state_description === $settings_description && ! $honeypot_field_id ) { | |
| 123 | - // 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. | |
| 124 | 115 | return; |
| 125 | 116 | } |
| 126 | 117 | |
| 127 | 118 | self::$instance->render_state_field(); |
| @@ -131,30 +122,23 @@ | ||
| 131 | 122 | * @return bool true if there is valid state data in the request. |
| 132 | 123 | */ |
| 133 | 124 | private static function get_state_from_request() { |
| 134 | 125 | $encrypted_state = FrmAppHelper::get_post_param( 'frm_state', '', 'sanitize_text_field' ); |
| 135 | - | |
| 136 | 126 | if ( ! $encrypted_state ) { |
| 137 | 127 | return false; |
| 138 | 128 | } |
| 139 | - | |
| 140 | 129 | $secret = self::get_encryption_secret(); |
| 141 | 130 | $decrypted_state = openssl_decrypt( $encrypted_state, 'AES-128-ECB', $secret ); |
| 142 | - | |
| 143 | 131 | if ( false === $decrypted_state ) { |
| 144 | 132 | return false; |
| 145 | 133 | } |
| 146 | - | |
| 147 | 134 | $decoded_state = json_decode( $decrypted_state, true ); |
| 148 | - | |
| 149 | 135 | if ( ! is_array( $decoded_state ) ) { |
| 150 | 136 | return false; |
| 151 | 137 | } |
| 152 | - | |
| 153 | 138 | foreach ( $decoded_state as $key => $value ) { |
| 154 | 139 | self::set_initial_value( self::decompressed_key( $key ), $value ); |
| 155 | 140 | } |
| 156 | - | |
| 157 | 141 | return true; |
| 158 | 142 | } |
| 159 | 143 | |
| 160 | 144 | /** |
| @@ -163,14 +147,13 @@ | ||
| 163 | 147 | public function render_state_field() { |
| 164 | 148 | if ( ! self::open_ssl_is_installed() ) { |
| 165 | 149 | return; |
| 166 | 150 | } |
| 167 | - | |
| 168 | 151 | if ( ! $this->state && ! self::get_state_from_request() ) { |
| 169 | 152 | return; |
| 170 | 153 | } |
| 171 | - | |
| 172 | - echo '<input name="frm_state" type="hidden" value="' . esc_attr( $this->get_state_string() ) . '" />'; | |
| 154 | + $state_string = $this->get_state_string(); | |
| 155 | + echo '<input name="frm_state" type="hidden" value="' . esc_attr( $state_string ) . '" />'; | |
| 173 | 156 | } |
| 174 | 157 | |
| 175 | 158 | /** |
| 176 | 159 | * @return string |
| @@ -178,12 +161,13 @@ | ||
| 178 | 161 | private function get_state_string() { |
| 179 | 162 | if ( ! self::open_ssl_is_installed() ) { |
| 180 | 163 | return ''; |
| 181 | 164 | } |
| 182 | - | |
| 183 | - $secret = self::get_encryption_secret(); | |
| 184 | - $json_encoded = json_encode( $this->compressed_state() ); | |
| 185 | - return openssl_encrypt( $json_encoded, 'AES-128-ECB', $secret ); | |
| 165 | + $secret = self::get_encryption_secret(); | |
| 166 | + $compressed_state = $this->compressed_state(); | |
| 167 | + $json_encoded = json_encode( $compressed_state ); | |
| 168 | + $encrypted = openssl_encrypt( $json_encoded, 'AES-128-ECB', $secret ); | |
| 169 | + return $encrypted; | |
| 186 | 170 | } |
| 187 | 171 | |
| 188 | 172 | /** |
| 189 | 173 | * Returns true if open SSL is installed. |
| @@ -188,9 +172,8 @@ | ||
| 188 | 172 | /** |
| 189 | 173 | * Returns true if open SSL is installed. |
| 190 | 174 | * |
| 191 | 175 | * @since 6.12 |
| 192 | - * | |
| 193 | 176 | * @return bool |
| 194 | 177 | */ |
| 195 | 178 | private static function open_ssl_is_installed() { |
| 196 | 179 | return function_exists( 'openssl_encrypt' ); |
| @@ -202,13 +185,11 @@ | ||
| 202 | 185 | * @return array |
| 203 | 186 | */ |
| 204 | 187 | private function compressed_state() { |
| 205 | 188 | $compressed = array(); |
| 206 | - | |
| 207 | 189 | foreach ( $this->state as $key => $value ) { |
| 208 | 190 | $compressed[ self::compressed_key( $key ) ] = $value; |
| 209 | 191 | } |
| 210 | - | |
| 211 | 192 | return $compressed; |
| 212 | 193 | } |
| 213 | 194 | |
| 214 | 195 | /** |
| @@ -216,9 +197,8 @@ | ||
| 216 | 197 | * "title" => "t". |
| 217 | 198 | * "description" => "d". |
| 218 | 199 | * |
| 219 | 200 | * @param string $key |
| 220 | - * | |
| 221 | 201 | * @return string |
| 222 | 202 | */ |
| 223 | 203 | private static function compressed_key( $key ) { |
| 224 | 204 | return $key[0]; |
| @@ -229,9 +209,8 @@ | ||
| 229 | 209 | * Pro supports additional keys include "i" for include_fields and "g" for get params. |
| 230 | 210 | * To avoid conflicts, we should not add "i" or "g" in Lite for another state property. |
| 231 | 211 | * |
| 232 | 212 | * @param string $key |
| 233 | - * | |
| 234 | 213 | * @return string The full key name if one is found. If nothing is found, the $key param is passed back. |
| 235 | 214 | */ |
| 236 | 215 | private static function decompressed_key( $key ) { |
| 237 | 216 | switch ( $key ) { |
| @@ -238,10 +217,8 @@ | ||
| 238 | 217 | case 'd': |
| 239 | 218 | return 'description'; |
| 240 | 219 | case 't': |
| 241 | 220 | return 'title'; |
| 242 | - case 'h': | |
| 243 | - return 'honeypot_field_id'; | |
| 244 | 221 | } |
| 245 | 222 | return $key; |
| 246 | 223 | } |
| 247 | 224 | |
| @@ -257,9 +234,9 @@ | ||
| 257 | 234 | } |
| 258 | 235 | |
| 259 | 236 | // We don't have a secret, so let's generate one. |
| 260 | 237 | $secret_key = is_callable( 'sodium_crypto_secretbox_keygen' ) ? sodium_crypto_secretbox_keygen() : wp_generate_password( 32, true, true ); |
| 261 | - update_option( 'frm_form_state_key', base64_encode( $secret_key ), false ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | |
| 238 | + update_option( 'frm_form_state_key', base64_encode( $secret_key ), 'no' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode | |
| 262 | 239 | |
| 263 | 240 | return $secret_key; |
| 264 | 241 | } |
| 265 | 242 | } |