Value => label, in the order the form lists them. */ public static function types(): array { return array( 'bug' => __( 'Bug Report', 'migratex' ), 'feature' => __( 'Feature Request', 'migratex' ), 'question' => __( 'General Question', 'migratex' ), ); } public function send( Request $request ): Response { // Validated as typed, not "repaired": sanitize_email() would quietly turn a malformed or // malicious value (line breaks, a second address) into some other valid-looking address. // is_email() already rejects spaces, commas and multiple @, and the check below rejects // anything that could start a new header line. $email = trim( $request->raw( 'email' ) ); if ( 1 === preg_match( '/[\r\n\x00<>,;]/', $email ) || ! is_email( $email ) ) { throw new HttpException( 'om_fb_email', esc_html__( 'Please enter a valid email address so we can reply.', 'migratex' ), 400 ); } $types = self::types(); $type = $request->str( 'type' ); if ( ! isset( $types[ $type ] ) ) { throw new HttpException( 'om_fb_type', esc_html__( 'Please choose what kind of feedback this is.', 'migratex' ), 400 ); } // Tags are removed and line breaks kept; control characters never reach the mail. $message = trim( (string) preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', sanitize_textarea_field( $request->raw( 'message' ) ) ) ); if ( '' === $message ) { throw new HttpException( 'om_fb_message', esc_html__( 'Please write a message.', 'migratex' ), 400 ); } if ( function_exists( 'mb_strlen' ) ? mb_strlen( $message ) > self::MAX_MESSAGE : strlen( $message ) > self::MAX_MESSAGE ) { throw new HttpException( 'om_fb_long', sprintf( /* translators: %s: character limit */ esc_html__( 'Please keep the message under %s characters.', 'migratex' ), esc_html( number_format_i18n( self::MAX_MESSAGE ) ) ), 400 ); } // The page sends the same id with every attempt at one message. If an earlier attempt got the // mail out but its reply was lost (a proxy error page), a retry through the other transport // must not send it a second time. $request_id = $request->str( 'request_id' ); $done_key = 1 === preg_match( '/^[a-f0-9-]{8,64}$/', $request_id ) ? 'migratex_fb_done_' . $request_id : ''; if ( '' !== $done_key && get_transient( $done_key ) ) { return Response::data( array( 'message' => __( 'Thank you! Your feedback has been sent.', 'migratex' ) ) ); } // A form that sends to a mailbox is a magnet for abuse (a stolen session, a script in a loop). $key = 'migratex_fb_' . get_current_user_id(); $sent = (int) get_transient( $key ); $limit = max( 1, (int) apply_filters( 'migratex_feedback_rate_limit', self::DEFAULT_LIMIT ) ); if ( $sent >= $limit ) { throw new HttpException( 'om_fb_rate', esc_html__( 'You have sent several messages in the last hour. Please wait a while, or use WhatsApp or email instead.', 'migratex' ), 429 ); } $label = $types[ $type ]; $site = site_url(); $subject = (string) preg_replace( '/[\r\n]+/', ' ', sprintf( '[MigrateX Feedback] %s from %s', $label, $site ) ); $fields = array( 'email' => $email, 'feedback_type' => $label, 'message' => $message, 'site_url' => $site, 'wp_version' => get_bloginfo( 'version' ), 'php_version' => PHP_VERSION, 'migratex_version' => MIGRATEX_VERSION, ); $reason = $this->post( $fields, $subject ); if ( '' !== $reason ) { /** * Fires when the feedback could not be delivered. * * @param string $reason Technical reason. Never shown to the user; contains no message text. */ do_action( 'migratex_feedback_failed', $reason ); // HTTP 200 with success:false, not a 5xx: a CDN or proxy would swap a 5xx body for its own // error page and the person would never see this message. The same words go to the person in the // fallback links, ready to send from their own mail program or phone. $text = implode( "\n", array( $message, '', '--', 'Type: ' . $label, 'From: ' . $email, 'Site: ' . $site, 'WordPress ' . $fields['wp_version'] . ', PHP ' . PHP_VERSION . ', MigrateX ' . MIGRATEX_VERSION ) ); return Response::failure( 'om_fb_send', __( 'Could not send your feedback from this site. You can send the same message by email or WhatsApp instead.', 'migratex' ), array( 'mailto' => Contact::mailtoUrl( $subject, $text ), 'whatsapp' => Contact::whatsappUrl( '[MigrateX ' . $label . '] ' . $message ), ) ); } if ( '' !== $done_key ) { set_transient( $done_key, 1, 10 * MINUTE_IN_SECONDS ); } // Only a message that actually left counts against the limit. set_transient( $key, $sent + 1, HOUR_IN_SECONDS ); return Response::data( array( 'message' => __( 'Thank you! Your feedback has been sent.', 'migratex' ) ) ); } /** * Posts the feedback to the configured endpoint, once, and says what went wrong. * * @param array $fields * @return string Empty on success, otherwise a short technical reason (no message text in it). */ private function post( array $fields, string $subject ): string { if ( ! Contact::feedbackConfigured() ) { return 'The feedback service is not configured.'; } $payload = $fields + array( 'subject' => $subject, 'from_name' => 'MigrateX Feedback', ); $key = Contact::feedbackAccessKey(); if ( '' !== $key ) { $payload['access_key'] = $key; } $json = wp_json_encode( $payload ); if ( false === $json ) { return 'The message could not be encoded.'; } $response = wp_remote_post( Contact::feedbackEndpoint(), array( 'timeout' => Contact::FEEDBACK_TIMEOUT, 'redirection' => 0, // an answer that redirects is not the service: never follow it with the message. 'user-agent' => 'MigrateX/' . MIGRATEX_VERSION, 'headers' => array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), 'body' => $json, ) ); if ( is_wp_error( $response ) ) { return 'Request failed: ' . $response->get_error_code(); } $code = (int) wp_remote_retrieve_response_code( $response ); if ( $code < 200 || $code > 299 ) { return 'The service answered HTTP ' . $code . '.'; } // Web3Forms and Formspree answer 200 with {"success":false,...} for a refused submission. $answer = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( is_array( $answer ) && array_key_exists( 'success', $answer ) && true !== $answer['success'] && 'true' !== $answer['success'] ) { return 'The service refused the message.'; } return ''; } }