plugin = $plugin; } /** * Register routes. */ public function register_routes(): void { $calculator_arg = array( 'calculator_id' => array( 'type' => 'integer', 'required' => true, 'sanitize_callback' => 'absint', ), ); // Public by design: these routes compute prices and accept new leads; they expose no stored data. // All routes are POST: page and REST caches (LiteSpeed caches REST GET by default) never store POST responses. register_rest_route( self::NAMESPACE, '/form-token', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'form_token' ), 'permission_callback' => '__return_true', ) ); register_rest_route( self::NAMESPACE, '/estimate', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'estimate' ), 'permission_callback' => '__return_true', 'args' => $calculator_arg, ) ); register_rest_route( self::NAMESPACE, '/leads', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'submit_lead' ), 'permission_callback' => '__return_true', 'args' => $calculator_arg, ) ); } /** * POST /form-token. */ public function form_token(): WP_REST_Response { if ( ! $this->plugin->security->hit( 'token' ) ) { return $this->no_cache( $this->rate_limited() ); } return $this->no_cache( new WP_REST_Response( array( 'token' => $this->plugin->security->issue_token() ), 200 ) ); } /** * POST /estimate. * * @param WP_REST_Request $request Request. */ public function estimate( WP_REST_Request $request ): WP_REST_Response { if ( ! $this->plugin->security->hit( 'estimate' ) ) { return $this->no_cache( $this->rate_limited() ); } $calculator = $this->active_calculator( (int) $request['calculator_id'] ); if ( ! $calculator ) { return $this->no_cache( $this->error( new WP_Error( 'thinroof_not_found', __( 'This calculator is not available.', 'thinroof' ), array( 'status' => 404 ) ) ) ); } $input = $this->plugin->validator->estimate_input( $this->params( $request ), $calculator ); if ( is_wp_error( $input ) ) { return $this->no_cache( $this->error( $input ) ); } if ( 'after_contact' === $calculator['config']['gate_estimate'] ) { // Inputs are valid; the price is revealed after contact details are submitted. return $this->no_cache( new WP_REST_Response( array( 'valid' => true ), 200 ) ); } $estimate = $this->plugin->engine->estimate( $input ); if ( is_wp_error( $estimate ) ) { return $this->no_cache( $this->error( $estimate ) ); } return $this->no_cache( new WP_REST_Response( array( 'valid' => true, 'estimate' => Pricing_Engine::public_view( $estimate ), ), 200 ) ); } /** * POST /leads. * * @param WP_REST_Request $request Request. */ public function submit_lead( WP_REST_Request $request ): WP_REST_Response { $security = $this->plugin->security; $params = $this->params( $request ); $calculator = $this->active_calculator( (int) $request['calculator_id'] ); if ( ! $calculator ) { return $this->no_cache( $this->error( new WP_Error( 'thinroof_not_found', __( 'This calculator is not available.', 'thinroof' ), array( 'status' => 404 ) ) ) ); } // Bots that fill the hidden field get a normal-looking success and nothing is stored. if ( Security::honeypot_tripped( $params ) ) { return $this->no_cache( new WP_REST_Response( array( 'success' => true, 'message' => $this->success_message( $calculator, '' ) ), 200 ) ); // phpcs:ignore WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound } $token = (string) ( $params['token'] ?? '' ); $verify = $security->verify_token( $token, (int) $this->plugin->settings->value( 'general', 'min_fill_seconds', 3 ) ); if ( is_wp_error( $verify ) ) { return $this->no_cache( $this->error( $verify ) ); } $validator = $this->plugin->validator; $project = $validator->estimate_input( $params, $calculator ); $contact = $validator->contact_input( $params, $calculator ); $fields = array(); foreach ( array( $project, $contact ) as $result ) { if ( is_wp_error( $result ) ) { $data = $result->get_error_data(); $fields = array_merge( $fields, (array) ( $data['fields'] ?? array() ) ); } } if ( $fields ) { return $this->no_cache( $this->error( new WP_Error( 'thinroof_invalid', __( 'Please check the highlighted fields.', 'thinroof' ), array( 'status' => 422, 'fields' => $fields, ) ) ) ); } // Counted only for well-formed submissions so visitors fixing typos are not locked out. if ( ! $security->hit( 'lead' ) ) { return $this->no_cache( $this->rate_limited() ); } if ( ! $security->consume_token( $token ) ) { return $this->no_cache( $this->error( new WP_Error( 'thinroof_token_used', __( 'This form was already submitted. Please reload the page to send another request.', 'thinroof' ), array( 'status' => 409 ) ) ) ); } // Duplicate suppression: same person, same calculator, within 10 minutes. if ( $this->plugin->leads->recent_duplicate( $contact['email'], (int) $calculator['id'], 10 ) ) { return $this->no_cache( new WP_REST_Response( array( 'success' => true, 'duplicate' => true, 'message' => $this->success_message( $calculator, $contact['full_name'] ) ), 200 ) ); // phpcs:ignore WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound } // Recompute on the server: client-side totals are never trusted. $estimate = $this->plugin->engine->estimate( $project ); if ( is_wp_error( $estimate ) ) { return $this->no_cache( $this->error( $estimate ) ); } $lead_id = $this->plugin->leads->create( array_merge( $project, $contact, array( 'calculator_id' => (int) $calculator['id'], 'estimate' => $estimate, 'consent_text' => $contact['marketing_consent'] ? (string) $calculator['config']['marketing_consent'] : '', 'source_url' => Security::same_site_url( $params['source_url'] ?? '' ), ) ) ); if ( ! $lead_id ) { return $this->no_cache( $this->error( new WP_Error( 'thinroof_save_failed', __( 'We could not save your request. Please try again or contact us directly.', 'thinroof' ), array( 'status' => 500 ) ) ) ); } return $this->no_cache( new WP_REST_Response( array( 'success' => true, 'message' => $this->success_message( $calculator, $contact['full_name'] ), 'estimate' => Pricing_Engine::public_view( $estimate ), ), 201 ) ); } /** * Active calculator or null. * * @param int $id ID. */ private function active_calculator( int $id ): ?array { $calculator = $this->plugin->calculators->get( $id ); return ( $calculator && 'active' === $calculator['status'] ) ? $calculator : null; } /** * Only scalar params, capped in length. * * @param WP_REST_Request $request Request. */ private function params( WP_REST_Request $request ): array { $out = array(); foreach ( (array) $request->get_params() as $key => $value ) { if ( is_scalar( $value ) ) { $out[ sanitize_key( (string) $key ) ] = substr( (string) $value, 0, 5000 ); } } return $out; } /** * Success message with {first_name}. * * @param array $calculator Calculator. * @param string $full_name Name. */ private function success_message( array $calculator, string $full_name ): string { $first = trim( (string) strtok( $full_name, ' ' ) ); $text = str_replace( '{first_name}', '' !== $first ? $first : __( 'there', 'thinroof' ), (string) $calculator['config']['success_message'] ); return wp_strip_all_tags( $text ); } /** * 429 response. */ private function rate_limited(): WP_REST_Response { return $this->error( new WP_Error( 'thinroof_rate_limited', __( 'Too many requests. Please wait a few minutes and try again.', 'thinroof' ), array( 'status' => 429 ) ) ); } /** * WP_Error to response with a stable shape: { code, message, fields }. * * @param WP_Error $error Error. */ private function error( WP_Error $error ): WP_REST_Response { $data = (array) $error->get_error_data(); $status = (int) ( $data['status'] ?? 400 ); return new WP_REST_Response( array( 'code' => $error->get_error_code(), 'message' => $error->get_error_message(), 'fields' => (object) ( $data['fields'] ?? array() ), ), $status ); } /** * Prevent page/edge caches from storing responses. * * @param WP_REST_Response $response Response. */ private function no_cache( WP_REST_Response $response ): WP_REST_Response { // Tell cache plugins directly, in addition to HTTP headers (LiteSpeed ignores Cache-Control from PHP). do_action( 'litespeed_control_set_nocache', 'ThinRoof dynamic endpoint' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's public API. if ( ! defined( 'DONOTCACHEPAGE' ) ) { define( 'DONOTCACHEPAGE', true ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- standard constant read by caching plugins. } foreach ( wp_get_nocache_headers() as $name => $value ) { if ( $value ) { $response->header( $name, $value ); } } $response->header( 'X-LiteSpeed-Cache-Control', 'no-cache' ); return $response; } }