'GET', 'callback' => [$this, 'get_settings'], 'permission_callback' => [$this, 'can_manage_settings'], ], [ 'methods' => 'PUT,POST', 'callback' => [$this, 'update_settings'], 'permission_callback' => [$this, 'can_manage_settings'], ]] ); register_rest_route( Api::NAMESPACE, '/settings/branding', [[ 'methods' => 'GET', 'callback' => static fn(): WP_REST_Response => new WP_REST_Response(self::branding(), 200), 'permission_callback' => [$this, 'can_manage_settings'], ], [ 'methods' => 'PUT,POST', 'callback' => [$this, 'update_branding'], 'permission_callback' => [$this, 'can_manage_settings'], ]] ); register_rest_route( Api::NAMESPACE, '/settings/test-mail', [[ 'methods' => 'POST', 'callback' => [$this, 'send_test_mail'], 'permission_callback' => [$this, 'can_manage_settings'], ]] ); register_rest_route( Api::NAMESPACE, '/settings/mail-log', [[ 'methods' => 'GET', 'callback' => [$this, 'get_mail_log'], 'permission_callback' => [$this, 'can_manage_settings'], ]] ); } public static function defaults() : array { $defaults = [ 'email' => [ 'from_name' => '', 'from_email' => '', 'default_recipients' => '', ], 'privacy' => [ 'retention_days' => 365, 'anonymize_ip' => true, 'store_user_agent' => false, 'consent_text' => '', ], 'spam' => [ 'honeypot' => false, 'min_completion_seconds' => 3, ], 'advanced' => [ 'delete_on_uninstall' => false, ], ]; return $defaults; } public static function all() : array { $stored = get_option( self::OPTION ); return self::merge( self::defaults(), ( is_array( $stored ) ? $stored : [] ) ); } public static function branding() : array { $defaults = [ 'primary_color' => '', 'button_text_color' => '', 'card_background' => '', 'text_color' => '', 'logo_url' => '', 'button_radius' => '', 'button_uppercase' => false, 'card_shadow' => 'soft', 'progress_style' => 'bar', 'font_source' => 'system', 'font_sizes' => [ 'heading' => [ 'min' => 0, 'max' => 0, ], 'body' => [ 'min' => 0, 'max' => 0, ], 'button' => [ 'min' => 0, 'max' => 0, ], 'checkbox' => [ 'min' => 0, 'max' => 0, ], ], ]; $stored = get_option( self::OPTION_BRANDING ); return self::merge( $defaults, ( is_array( $stored ) ? $stored : [] ) ); } public function get_settings() : WP_REST_Response { $settings = self::all(); return new WP_REST_Response($settings, 200); } public function update_settings( WP_REST_Request $request ) : WP_REST_Response|WP_Error { $incoming = $request->get_json_params(); if ( !is_array( $incoming ) ) { return new WP_Error('funnelnode_invalid', __( 'Invalid request.', 'funnelnode' ), [ 'status' => 400, ]); } $current = self::all(); $next = self::merge( $current, $incoming ); $email =& $next['email']; $email['from_name'] = sanitize_text_field( (string) $email['from_name'] ); $email['from_email'] = sanitize_email( (string) $email['from_email'] ); $email['default_recipients'] = sanitize_text_field( (string) $email['default_recipients'] ); unset($email); $privacy =& $next['privacy']; $privacy['retention_days'] = max( 0, (int) $privacy['retention_days'] ); $privacy['anonymize_ip'] = (bool) $privacy['anonymize_ip']; $privacy['store_user_agent'] = (bool) $privacy['store_user_agent']; $privacy['consent_text'] = sanitize_textarea_field( (string) $privacy['consent_text'] ); unset($privacy); $next['spam']['honeypot'] = (bool) $next['spam']['honeypot']; $next['spam']['min_completion_seconds'] = max( 0, (int) $next['spam']['min_completion_seconds'] ); $next['advanced']['delete_on_uninstall'] = (bool) $next['advanced']['delete_on_uninstall']; $stored = get_option( self::OPTION ); update_option( self::OPTION, array_replace_recursive( ( is_array( $stored ) ? $stored : [] ), $next ), false ); return $this->get_settings(); } public function update_branding( WP_REST_Request $request ) : WP_REST_Response|WP_Error { $incoming = $request->get_json_params(); if ( !is_array( $incoming ) ) { return new WP_Error('funnelnode_invalid', __( 'Invalid request.', 'funnelnode' ), [ 'status' => 400, ]); } $next = self::merge( self::branding(), $incoming ); $next['primary_color'] = $this->sanitize_color( (string) $next['primary_color'] ); $next['button_text_color'] = $this->sanitize_color( (string) $next['button_text_color'] ); $next['card_background'] = $this->sanitize_color( (string) $next['card_background'] ); $next['text_color'] = $this->sanitize_color( (string) $next['text_color'] ); $next['logo_url'] = esc_url_raw( (string) $next['logo_url'] ); $next['button_radius'] = ( preg_match( '/^\\d{1,3}(px|rem|em|%)?$/', (string) $next['button_radius'] ) ? (string) $next['button_radius'] : '' ); $next['button_uppercase'] = !empty( $next['button_uppercase'] ); $next['card_shadow'] = ( in_array( $next['card_shadow'], ['none', 'soft', 'strong'], true ) ? (string) $next['card_shadow'] : 'soft' ); $next['progress_style'] = ( in_array( $next['progress_style'], ['bar', 'dots'], true ) ? (string) $next['progress_style'] : 'bar' ); $next['font_source'] = ( in_array( $next['font_source'], ['system', 'theme'], true ) ? (string) $next['font_source'] : 'system' ); $next['font_sizes'] = $this->sanitize_font_sizes( $next['font_sizes'] ?? [] ); update_option( self::OPTION_BRANDING, $next, false ); return new WP_REST_Response($next, 200); } public function send_test_mail( WP_REST_Request $request ) : WP_REST_Response { $recipient = sanitize_email( (string) $request->get_param( 'recipient' ) ); if ( '' === $recipient ) { $recipient = wp_get_current_user()->user_email; } $text = __( "This is a test email from FunnelNode.\nIf you are reading this, your email settings work.", 'funnelnode' ); do_action( 'funnelnode_test_mail_before' ); $sent = Mailer::send( [$recipient], __( 'FunnelNode test email', 'funnelnode' ), Mailer::wrap_template( nl2br( esc_html( $text ) ), __( 'Test email', 'funnelnode' ) ), [ 'html' => true, 'context' => 'test', ] ); $response = [ 'ok' => $sent, 'recipient' => $recipient, 'error' => ( $sent ? '' : Mailer::last_error() ), ]; return new WP_REST_Response((array) apply_filters( 'funnelnode_test_mail_response', $response ), 200); } public function get_mail_log() : WP_REST_Response { global $wpdb; $table = Migrations::table( Migrations::TABLE_MAIL_LOG ); $rows = ( $wpdb->get_results( "SELECT id, recipient, subject, status, error, funnel_id, created_at\n FROM {$table} ORDER BY id DESC LIMIT 20", // phpcs:ignore ARRAY_A ) ?: [] ); return new WP_REST_Response($rows, 200); } private static function merge( array $base, array $incoming ) : array { foreach ( $base as $key => $value ) { if ( !array_key_exists( $key, $incoming ) ) { continue; } if ( is_array( $value ) && self::is_assoc( $value ) ) { $base[$key] = self::merge( $value, ( is_array( $incoming[$key] ) ? $incoming[$key] : [] ) ); } else { $base[$key] = $incoming[$key]; } } return $base; } private static function is_assoc( array $value ) : bool { return array_keys( $value ) !== range( 0, count( $value ) - 1 ); } private function sanitize_color( string $color ) : string { $color = trim( $color ); return ( preg_match( '/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', $color ) ? strtolower( $color ) : '' ); } private function sanitize_font_sizes( $raw ) : array { $clean = []; foreach ( [ 'heading', 'body', 'button', 'checkbox' ] as $key ) { $min = (int) (( is_array( $raw ) ? $raw[$key]['min'] ?? 0 : 0 )); $max = (int) (( is_array( $raw ) ? $raw[$key]['max'] ?? 0 : 0 )); $min = ( $min >= 8 && $min <= 120 ? $min : 0 ); $max = ( $max >= 8 && $max <= 120 ? $max : 0 ); if ( $min && $max && $min > $max ) { [$min, $max] = [$max, $min]; } $clean[$key] = [ 'min' => $min, 'max' => $max, ]; } return $clean; } }