'known', 'require_consent' => false, 'exclude_roles' => array( 'administrator', 'editor', 'shop_manager' ), 'anon_retention' => 30, 'pageview_retention' => 0, 'auto_company' => true, 'update_policy' => 'fill', 'default_status' => 'lead', 'forms' => Nodebook_Forms::keys(), 'capture_registrations' => true, 'woo_sync' => true, 'delete_on_uninstall' => false, 'attribution' => true, 'intent_pages' => '', 'niche' => '', 'clicks' => true, 'bookings' => true, 'team_roles' => array(), 'default_owner' => 0, 'visibility' => 'all', 'can_export' => 'all', 'can_delete' => 'all', 'alert_mode' => 'intent', 'alert_email' => '', 'digest' => true, 'weekly' => true, 'weekly_day' => 1, 'weekly_email' => '', 'quiet' => true, 'snooze' => 'early', 'currency' => '', 'templates' => array(), 'any_form' => false, 'node_personality' => 'playful', 'node_email' => false, 'node_owner' => 0, 'team_size' => '', 'woo_capture' => '', 'woo_history' => true, 'node_growth' => true, 'deals' => true, 'full_screen' => true, 'crm_only' => false, 'node_email_time' => '07:30', 'node_email_days' => array( 0, 1, 2, 3, 4, 5, 6 ), ); } public static function all() { $saved = get_option( self::OPTION, array() ); return wp_parse_args( is_array( $saved ) ? $saved : array(), self::defaults() ); } public static function get( $key ) { $all = self::all(); return isset( $all[ $key ] ) ? $all[ $key ] : null; } /** * One path pattern per line, e.g. "/pricing*". Max 50 lines. */ public static function clean_patterns( $raw ) { $out = array(); foreach ( preg_split( '/[\r\n]+/', is_array( $raw ) ? implode( "\n", $raw ) : (string) $raw ) as $line ) { $line = trim( sanitize_text_field( $line ) ); if ( '' === $line ) { continue; } if ( '/' !== $line[0] ) { $path = wp_parse_url( $line, PHP_URL_PATH ); $line = $path ? $path . ( '*' === substr( $line, -1 ) && '*' !== substr( $path, -1 ) ? '*' : '' ) : '/' . $line; } $out[] = mb_substr( $line, 0, 200 ); if ( count( $out ) >= 50 ) { break; } } return implode( "\n", array_unique( $out ) ); } /** "a@x.com, b@y.com" → valid addresses only, comma-separated. Also accepts ; and new lines. */ public static function clean_emails( $raw ) { $list = preg_split( '/[,;\s]+/', (string) $raw ); return implode( ', ', array_unique( array_filter( array_map( 'sanitize_email', $list ), 'is_email' ) ) ); } /** Reply templates: up to 10 named texts with {first_name}, {item}, {site} and {my_name} placeholders. */ public static function clean_templates( $rows ) { $out = array(); foreach ( is_array( $rows ) ? $rows : array() as $r ) { if ( ! is_array( $r ) ) { continue; } $name = mb_substr( trim( sanitize_text_field( (string) ( $r['name'] ?? '' ) ) ), 0, 60 ); $text = mb_substr( trim( sanitize_textarea_field( (string) ( $r['text'] ?? '' ) ) ), 0, 1000 ); if ( '' !== $text ) { $out[] = array( 'name' => '' !== $name ? $name : wp_trim_words( $text, 4, '…' ), 'text' => $text, ); } if ( count( $out ) >= 10 ) { break; } } return $out; } /** Days of the week as 0 (Sunday) … 6, in order; nothing valid gives the default. */ private static function weekdays( $in, $fallback ) { if ( ! is_array( $in ) ) { return $fallback; } $out = array(); foreach ( $in as $v ) { if ( is_int( $v ) || ( is_string( $v ) && ctype_digit( $v ) ) ) { $v = (int) $v; if ( $v >= 0 && $v <= 6 ) { $out[ $v ] = $v; } } } ksort( $out ); return $out ? array_values( $out ) : $fallback; } public static function sanitize( $in ) { $in = is_array( $in ) ? $in : array(); $d = self::defaults(); $roles = function_exists( 'wp_roles' ) ? array_keys( wp_roles()->roles ) : array(); $flag = static function ( $k ) use ( $in ) { return ! empty( $in[ $k ] ) && '0' !== $in[ $k ]; }; $pick = static function ( $k, array $allowed ) use ( $in, $d ) { $v = isset( $in[ $k ] ) && is_string( $in[ $k ] ) ? sanitize_key( $in[ $k ] ) : ''; return in_array( $v, $allowed, true ) ? $v : $d[ $k ]; }; // Deal stages live in their own option; the settings form carries them so one Save covers both. if ( isset( $in['deal_stages'] ) && class_exists( 'Nodebook_Admin_Deals' ) ) { Nodebook_Admin_Deals::stages_from_settings( $in['deal_stages'] ); } return array( 'tracking_mode' => $pick( 'tracking_mode', array( 'off', 'known', 'all' ) ), 'require_consent' => $flag( 'require_consent' ), 'exclude_roles' => array_values( array_intersect( array_map( 'sanitize_key', (array) ( $in['exclude_roles'] ?? array() ) ), $roles ) ), 'anon_retention' => max( 1, min( 365, absint( $in['anon_retention'] ?? $d['anon_retention'] ) ) ), 'pageview_retention' => min( 3650, absint( $in['pageview_retention'] ?? 0 ) ), 'auto_company' => $flag( 'auto_company' ), 'update_policy' => $pick( 'update_policy', array( 'fill', 'overwrite' ) ), 'default_status' => $pick( 'default_status', array_keys( Nodebook_Fields::statuses() ) ), 'forms' => array_values( array_intersect( array_map( 'sanitize_key', (array) ( $in['forms'] ?? array() ) ), Nodebook_Forms::keys() ) ), 'capture_registrations' => $flag( 'capture_registrations' ), // Older switch, kept in step with "When buyers become contacts" (woo_capture). 'woo_sync' => in_array( $in['woo_capture'] ?? '', array( 'orders', 'paid', 'existing', 'off' ), true ) ? 'off' !== $in['woo_capture'] : ( isset( $in['woo_sync'] ) ? $flag( 'woo_sync' ) : true ), 'delete_on_uninstall' => $flag( 'delete_on_uninstall' ), 'attribution' => $flag( 'attribution' ), 'intent_pages' => self::clean_patterns( $in['intent_pages'] ?? '' ), 'alert_mode' => $pick( 'alert_mode', array( 'off', 'intent', 'any' ) ), 'alert_email' => self::clean_emails( $in['alert_email'] ?? '' ), 'digest' => $flag( 'digest' ), 'weekly' => $flag( 'weekly' ), 'weekly_day' => min( 6, absint( $in['weekly_day'] ?? $d['weekly_day'] ) ), 'weekly_email' => self::clean_emails( $in['weekly_email'] ?? '' ), 'quiet' => $flag( 'quiet' ), 'snooze' => $pick( 'snooze', array( 'early', 'strict' ) ), 'templates' => self::clean_templates( $in['templates'] ?? array() ), 'currency' => preg_match( '/^[A-Za-z]{3}$/', trim( (string) ( $in['currency'] ?? '' ) ) ) ? strtoupper( trim( (string) $in['currency'] ) ) : '', 'any_form' => $flag( 'any_form' ), 'niche' => ( isset( $in['niche'] ) && isset( Nodebook_Niche::all()[ sanitize_key( (string) $in['niche'] ) ] ) ) ? sanitize_key( (string) $in['niche'] ) : '', 'clicks' => $flag( 'clicks' ), 'bookings' => $flag( 'bookings' ), 'team_roles' => array_values( array_intersect( array_map( 'sanitize_key', (array) ( $in['team_roles'] ?? array() ) ), array_keys( Nodebook_Team::eligible_roles() ) ) ), 'default_owner' => absint( $in['default_owner'] ?? 0 ), 'visibility' => $pick( 'visibility', array( 'all', 'own_unassigned', 'own' ) ), 'can_export' => $pick( 'can_export', array( 'all', 'admins' ) ), 'can_delete' => $pick( 'can_delete', array( 'all', 'admins' ) ), 'node_personality' => $pick( 'node_personality', array( 'playful', 'calm', 'off' ) ), 'node_email' => $flag( 'node_email' ), 'node_owner' => absint( $in['node_owner'] ?? 0 ), 'team_size' => in_array( $in['team_size'] ?? '', array( 'solo', 'team' ), true ) ? $in['team_size'] : '', 'woo_capture' => in_array( $in['woo_capture'] ?? '', array( 'orders', 'paid', 'existing', 'off' ), true ) ? $in['woo_capture'] : '', 'woo_history' => $flag( 'woo_history' ), 'node_growth' => isset( $in['node_growth'] ) ? $flag( 'node_growth' ) : true, 'deals' => isset( $in['deals'] ) ? $flag( 'deals' ) : true, 'full_screen' => isset( $in['full_screen'] ) ? $flag( 'full_screen' ) : true, 'crm_only' => isset( $in['crm_only'] ) ? $flag( 'crm_only' ) : false, 'node_email_time' => isset( $in['node_email_time'] ) && preg_match( '/^([01]\d|2[0-3]):[0-5]\d$/', (string) $in['node_email_time'] ) ? (string) $in['node_email_time'] : $d['node_email_time'], 'node_email_days' => self::weekdays( $in['node_email_days'] ?? null, $d['node_email_days'] ), ); } }