*/ private array $cache = array(); /** * Option name for a group. * * @param string $group Group key. */ public static function option_name( string $group ): string { return 'thinroof_' . $group; } /** * Get a merged group. * * @param string $group Group key. * @return array */ public function get( string $group ): array { if ( isset( $this->cache[ $group ] ) ) { return $this->cache[ $group ]; } $stored = get_option( self::option_name( $group ), array() ); $stored = is_array( $stored ) ? $stored : array(); $merged = array_merge( $this->defaults( $group ), $stored ); $this->cache[ $group ] = $merged; return $merged; } /** * Get one value. * * @param string $group Group. * @param string $key Key. * @param mixed $default Fallback. * @return mixed */ public function value( string $group, string $key, $default = null ) { $all = $this->get( $group ); return array_key_exists( $key, $all ) ? $all[ $key ] : $default; } /** * Sanitise and save a group. * * @param string $group Group key. * @param array $input Raw input (unslashed). * @return array Saved values. */ public function save( string $group, array $input ): array { $method = 'sanitize_' . $group; $clean = method_exists( $this, $method ) ? $this->{$method}( $input ) : array(); update_option( self::option_name( $group ), $clean, 'email' !== $group ); unset( $this->cache[ $group ] ); if ( 'pricing' === $group ) { update_option( self::OPTION_PRICING_REVIEWED, 1, true ); } return $clean; } /** * Install defaults if missing. */ public function install_defaults(): void { foreach ( self::GROUPS as $group ) { if ( false === get_option( self::option_name( $group ), false ) ) { add_option( self::option_name( $group ), $this->defaults( $group ), '', 'email' !== $group ); } } add_option( self::OPTION_PRICING_REVIEWED, 0 ); } /** * Clear request cache (after external update_option calls). */ public function flush(): void { $this->cache = array(); } /** * Whether the admin has saved the Pricing screen at least once. */ public function pricing_reviewed(): bool { return (bool) get_option( self::OPTION_PRICING_REVIEWED, 0 ); } /* ------------------------------------------------------------------ * Defaults * ------------------------------------------------------------------ */ /** * Defaults for a group. * * @param string $group Group key. * @return array */ public function defaults( string $group ): array { switch ( $group ) { case 'general': $defaults = array( 'business_name' => wp_specialchars_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES ), 'logo_id' => 0, 'primary_color' => '#0b2545', 'secondary_color' => '#1d4ed8', 'button_color' => '#1d4ed8', 'button_text_color' => '#ffffff', 'font_size' => 16, 'border_radius' => 8, 'default_intro' => __( 'Answer a few quick questions to get a preliminary estimate for your roofing project.', 'thinroof' ), 'default_disclaimer' => __( 'This is a preliminary estimate based on the details you entered and our standard pricing. It is not a binding quote. Your final price depends on an on-site inspection, local conditions and material availability.', 'thinroof' ), 'privacy_page_id' => (int) get_option( 'wp_page_for_privacy_policy', 0 ), 'rate_limit_leads' => 5, 'rate_limit_estimates' => 60, 'min_fill_seconds' => 3, 'retention_days' => 0, 'delete_on_uninstall' => 0, ); break; case 'pricing': $defaults = array( 'area_basis' => 'footprint', 'waste_pct' => 10, 'tearoff_per_square' => 100, 'disposal_per_square' => 40, 'min_project_price' => 1500, 'range_pct' => 10, 'pitches' => array( array( 'key' => 'flat', 'label' => __( 'Flat or nearly flat (0–2/12)', 'thinroof' ), 'area_factor' => 1.00, 'labor_factor' => 1.00, ), array( 'key' => 'low', 'label' => __( 'Low slope (3/12–5/12)', 'thinroof' ), 'area_factor' => 1.05, 'labor_factor' => 1.00, ), array( 'key' => 'medium', 'label' => __( 'Medium slope (6/12–8/12)', 'thinroof' ), 'area_factor' => 1.16, 'labor_factor' => 1.10, ), array( 'key' => 'steep', 'label' => __( 'Steep (9/12–12/12)', 'thinroof' ), 'area_factor' => 1.30, 'labor_factor' => 1.25, ), array( 'key' => 'very_steep', 'label' => __( 'Very steep (over 12/12)', 'thinroof' ), 'area_factor' => 1.50, 'labor_factor' => 1.45, ), ), 'stories' => array( array( 'key' => '1', 'label' => __( '1 story', 'thinroof' ), 'factor' => 1.00, ), array( 'key' => '2', 'label' => __( '2 stories', 'thinroof' ), 'factor' => 1.10, ), array( 'key' => '3', 'label' => __( '3 or more stories', 'thinroof' ), 'factor' => 1.20, ), ), 'property_types' => array( array( 'key' => 'single_family', 'label' => __( 'Single-family home', 'thinroof' ), 'multiplier' => 1.00, ), array( 'key' => 'townhouse', 'label' => __( 'Townhouse or duplex', 'thinroof' ), 'multiplier' => 1.00, ), array( 'key' => 'multi_family', 'label' => __( 'Multi-family building', 'thinroof' ), 'multiplier' => 1.05, ), array( 'key' => 'commercial', 'label' => __( 'Commercial building', 'thinroof' ), 'multiplier' => 1.10, ), ), 'project_types' => array( array( 'key' => 'replacement', 'label' => __( 'Roof replacement', 'thinroof' ), 'mode' => 'area', 'multiplier' => 1.00, 'flat_amount' => 0, 'allow_tear_off' => 1, ), array( 'key' => 'new_install', 'label' => __( 'New roof installation', 'thinroof' ), 'mode' => 'area', 'multiplier' => 1.00, 'flat_amount' => 0, 'allow_tear_off' => 0, ), array( 'key' => 'repair', 'label' => __( 'Roof repair', 'thinroof' ), 'mode' => 'flat', 'multiplier' => 1.00, 'flat_amount' => 450, 'allow_tear_off' => 0, ), array( 'key' => 'inspection', 'label' => __( 'Storm damage inspection', 'thinroof' ), 'mode' => 'flat', 'multiplier' => 1.00, 'flat_amount' => 0, 'allow_tear_off' => 0, ), ), ); break; case 'email': $defaults = array( 'business_name' => '', 'from_name' => '', 'reply_to' => '', 'admin_enabled' => 1, 'admin_recipients' => (string) get_option( 'admin_email' ), 'admin_subject' => __( 'New roofing lead: {full_name} ({zip})', 'thinroof' ), 'admin_template' => self::default_admin_template(), 'visitor_enabled' => 1, 'visitor_subject' => __( 'Your roofing estimate from {business_name}', 'thinroof' ), 'visitor_template' => self::default_visitor_template(), ); break; default: $defaults = array(); } /** * Filter defaults for a settings group. * * @param array $defaults Defaults. * @param string $group Group key. */ return (array) apply_filters( 'thinroof_settings_defaults', $defaults, $group ); } /** * Default admin email body. */ public static function default_admin_template(): string { return __( "

You have a new lead from your roofing calculator.

\n

Name: {full_name}
\nEmail: {email}
\nPhone: {phone}
\nZIP code: {zip}

\n

Project: {project_type}
\nProperty: {property_type}
\nMaterial: {material}
\nRoof area: {roof_area} sq ft ({roof_squares} squares)
\nExisting roof removal: {tear_off}
\nEstimate: {estimate_range}

\n

Budget: {budget}
\nTimeline: {timeline}
\nMarketing consent: {marketing_consent}

\n

Notes:
{notes}

\n

View this lead in WordPress

", 'thinroof' ); } /** * Default visitor email body. */ public static function default_visitor_template(): string { return __( "

Hi {first_name},

\n

Thanks for requesting a roofing estimate from {business_name}. We received your details and will contact you shortly.

\n

Project: {project_type}
\nMaterial: {material}
\nEstimated roof area: {roof_area} sq ft
\nPreliminary estimate: {estimate_range}

\n

{disclaimer}

\n

{business_name}

", 'thinroof' ); } /* ------------------------------------------------------------------ * Sanitisers * ------------------------------------------------------------------ */ /** * Sanitise general settings. * * @param array $in Input. */ private function sanitize_general( array $in ): array { $d = $this->defaults( 'general' ); return array( 'business_name' => sanitize_text_field( $in['business_name'] ?? $d['business_name'] ), 'logo_id' => absint( $in['logo_id'] ?? 0 ), 'primary_color' => self::color( $in['primary_color'] ?? '', $d['primary_color'] ), 'secondary_color' => self::color( $in['secondary_color'] ?? '', $d['secondary_color'] ), 'button_color' => self::color( $in['button_color'] ?? '', $d['button_color'] ), 'button_text_color' => self::color( $in['button_text_color'] ?? '', $d['button_text_color'] ), 'font_size' => self::int_range( $in['font_size'] ?? 16, 12, 24 ), 'border_radius' => self::int_range( $in['border_radius'] ?? 8, 0, 32 ), 'default_intro' => sanitize_textarea_field( $in['default_intro'] ?? '' ), 'default_disclaimer' => sanitize_textarea_field( $in['default_disclaimer'] ?? $d['default_disclaimer'] ), 'privacy_page_id' => absint( $in['privacy_page_id'] ?? 0 ), 'rate_limit_leads' => self::int_range( $in['rate_limit_leads'] ?? 5, 1, 100 ), 'rate_limit_estimates' => self::int_range( $in['rate_limit_estimates'] ?? 60, 5, 1000 ), 'min_fill_seconds' => self::int_range( $in['min_fill_seconds'] ?? 3, 0, 60 ), 'retention_days' => self::int_range( $in['retention_days'] ?? 0, 0, 3650 ), 'delete_on_uninstall' => empty( $in['delete_on_uninstall'] ) ? 0 : 1, ); } /** * Sanitise pricing settings. * * @param array $in Input. */ private function sanitize_pricing( array $in ): array { $d = $this->defaults( 'pricing' ); $out = array( 'area_basis' => in_array( $in['area_basis'] ?? '', array( 'footprint', 'roof' ), true ) ? $in['area_basis'] : 'footprint', 'waste_pct' => self::num_range( $in['waste_pct'] ?? 10, 0, 50 ), 'tearoff_per_square' => self::money( $in['tearoff_per_square'] ?? 0 ), 'disposal_per_square' => self::money( $in['disposal_per_square'] ?? 0 ), 'min_project_price' => self::money( $in['min_project_price'] ?? 0 ), 'range_pct' => self::num_range( $in['range_pct'] ?? 10, 0, 50 ), ); $out['pitches'] = self::rows( $in['pitches'] ?? array(), $d['pitches'], static fn( array $r ): array => array( 'area_factor' => self::num_range( $r['area_factor'] ?? 1, 1, 3 ), 'labor_factor' => self::num_range( $r['labor_factor'] ?? 1, 0.5, 3 ), ) ); $out['stories'] = self::rows( $in['stories'] ?? array(), $d['stories'], static fn( array $r ): array => array( 'factor' => self::num_range( $r['factor'] ?? 1, 0.5, 3 ) ) ); $out['property_types'] = self::rows( $in['property_types'] ?? array(), $d['property_types'], static fn( array $r ): array => array( 'multiplier' => self::num_range( $r['multiplier'] ?? 1, 0.1, 5 ) ) ); $out['project_types'] = self::rows( $in['project_types'] ?? array(), $d['project_types'], static fn( array $r ): array => array( 'mode' => in_array( $r['mode'] ?? '', array( 'area', 'flat' ), true ) ? $r['mode'] : 'area', 'multiplier' => self::num_range( $r['multiplier'] ?? 1, 0.1, 5 ), 'flat_amount' => self::money( $r['flat_amount'] ?? 0 ), 'allow_tear_off' => empty( $r['allow_tear_off'] ) ? 0 : 1, ) ); return $out; } /** * Sanitise email settings. * * @param array $in Input. */ private function sanitize_email( array $in ): array { $recipients = array(); foreach ( preg_split( '/[\s,;]+/', (string) ( $in['admin_recipients'] ?? '' ) ) as $address ) { $address = sanitize_email( $address ); if ( $address && is_email( $address ) ) { $recipients[] = $address; } } $reply_to = sanitize_email( (string) ( $in['reply_to'] ?? '' ) ); return array( 'business_name' => sanitize_text_field( $in['business_name'] ?? '' ), 'from_name' => self::header_text( $in['from_name'] ?? '' ), 'reply_to' => is_email( $reply_to ) ? $reply_to : '', 'admin_enabled' => empty( $in['admin_enabled'] ) ? 0 : 1, 'admin_recipients' => implode( ', ', array_unique( $recipients ) ), 'admin_subject' => self::header_text( $in['admin_subject'] ?? '' ), 'admin_template' => wp_kses_post( (string) ( $in['admin_template'] ?? '' ) ), 'visitor_enabled' => empty( $in['visitor_enabled'] ) ? 0 : 1, 'visitor_subject' => self::header_text( $in['visitor_subject'] ?? '' ), 'visitor_template' => wp_kses_post( (string) ( $in['visitor_template'] ?? '' ) ), ); } /* ------------------------------------------------------------------ * Primitive helpers (public static so other classes and tests reuse them) * ------------------------------------------------------------------ */ /** * Merge editable rows onto the canonical list by key. Labels are editable, * keys and the set of rows are fixed in Free. * * @param mixed $input Submitted rows keyed by row key. * @param array $defaults Canonical rows. * @param callable $numeric Builds the numeric part of a row. * @return array */ public static function rows( $input, array $defaults, callable $numeric ): array { $input = is_array( $input ) ? $input : array(); $out = array(); foreach ( $defaults as $row ) { $submitted = isset( $input[ $row['key'] ] ) && is_array( $input[ $row['key'] ] ) ? $input[ $row['key'] ] : $row; $label = sanitize_text_field( (string) ( $submitted['label'] ?? '' ) ); $out[] = array_merge( array( 'key' => $row['key'], 'label' => '' !== $label ? $label : $row['label'], ), $numeric( $submitted ) ); } return $out; } /** * Hex colour or fallback. * * @param mixed $value Input. * @param string $fallback Fallback. */ public static function color( $value, string $fallback ): string { $color = sanitize_hex_color( (string) $value ); return $color ? $color : $fallback; } /** * Clamp integer. * * @param mixed $value Input. * @param int $min Min. * @param int $max Max. */ public static function int_range( $value, int $min, int $max ): int { return max( $min, min( $max, (int) $value ) ); } /** * Clamp float, rounded to 2 dp. * * @param mixed $value Input. * @param float $min Min. * @param float $max Max. */ public static function num_range( $value, float $min, float $max ): float { $value = is_numeric( $value ) ? (float) $value : $min; return round( max( $min, min( $max, $value ) ), 2 ); } /** * Non-negative money value, strips $ and commas. * * @param mixed $value Input. */ public static function money( $value ): float { $value = str_replace( array( '$', ',', ' ' ), '', (string) $value ); return is_numeric( $value ) ? round( max( 0, min( 9999999, (float) $value ) ), 2 ) : 0.0; } /** * Single-line text safe for email headers. * * @param mixed $value Input. */ public static function header_text( $value ): string { return trim( str_replace( array( "\r", "\n", '%0a', '%0d' ), ' ', sanitize_text_field( (string) $value ) ) ); } }