is_use_csrf() ) { return $html; } return ( $html . Csrf_Tools::get_field() ); } /** * @param array $request * * @return array * @throws Spam_Exception */ public function handle_request( array $request ): array { if ( ! jet_fb_live_args()->is_use_csrf() ) { return $request; } $token = $request[ Csrf_Tools::FIELD ] ?? false; $this->client_id = Csrf_Tools::client_id( jet_fb_live()->form_id ); // delete all old tokens Csrf_Token_Model::clear(); if ( ! Csrf_Tools::consume( $token, $this->client_id ) ) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped throw new Spam_Exception( self::SPAM_EXCEPTION ); } add_action( 'jet-form-builder/form-handler/after-send', array( $this, 'handle_after_send' ), 10, 2 ); return $request; } public function handle_after_send( $handler, bool $is_success ) { remove_action( 'jet-form-builder/form-handler/after-send', array( $this, 'handle_after_send' ), 10 ); if ( ! $handler->is_ajax() ) { return; } try { $token = Csrf_Tools::add( Csrf_Tools::generate(), $this->client_id ); } catch ( \Exception $exception ) { return; } $handler->add_response_data( array( Csrf_Tools::FIELD => $token, ) ); } public function handle_messages( array $messages ): array { $messages[ self::SPAM_EXCEPTION ] = array( 'label' => __( 'CSRF token validation failed', 'jet-form-builder' ), 'value' => __( 'Invalid token', 'jet-form-builder' ), ); return $messages; } }