'string', 'sanitize_callback' => array( $this, 'sanitize_site_token' ), 'default' => '', ) ); register_setting( 'krawlup_settings_group', 'krawlup_custom_domain', array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_custom_domain' ), 'default' => KRAWLUP_DEFAULT_ENDPOINT, ) ); register_setting( 'krawlup_settings_group', 'krawlup_track_admin', array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_checkbox' ), 'default' => '0', ) ); register_setting( 'krawlup_settings_group', 'krawlup_track_woocommerce', array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_checkbox' ), 'default' => '1', ) ); } /** * Sanitize and validate Krawlup site token (8-64 hex chars). * * @param string $token Raw token. * @return string Sanitized token. */ public function sanitize_site_token( $token ) { $clean = strtolower( trim( sanitize_text_field( $token ) ) ); if ( empty( $clean ) ) { return ''; } if ( ! preg_match( '/^[0-9a-f]{8,64}$/', $clean ) ) { add_settings_error( 'krawlup_site_token', 'invalid_token', __( 'Invalid Site Token format. The Krawlup token should be an 8–64 character hexadecimal string.', 'krawlup' ), 'error' ); return get_option( 'krawlup_site_token', '' ); } return $clean; } /** * Sanitize custom endpoint domain. * * @param string $domain Raw URL or domain. * @return string Sanitized URL. */ public function sanitize_custom_domain( $domain ) { $clean = trim( sanitize_text_field( $domain ) ); if ( empty( $clean ) ) { return KRAWLUP_DEFAULT_ENDPOINT; } // https:// is mandatory: this URL becomes every visitor's script src. // Plain HTTP is rewritten (browsers block it on secure sites as mixed // content, silently stopping tracking), and anything that does not // survive sanitization as https falls back to the default endpoint // rather than landing as a broken or exotic-scheme URL. if ( preg_match( '#^http://#i', $clean ) ) { $clean = 'https://' . substr( $clean, 7 ); add_settings_error( 'krawlup_custom_domain', 'insecure_endpoint', __( 'Custom endpoints must use HTTPS — the http:// URL was rewritten to https://. Browsers block plain-HTTP scripts on secure sites (mixed content), which would silently stop tracking.', 'krawlup' ), 'warning' ); } elseif ( ! preg_match( '#^https://#i', $clean ) ) { $clean = 'https://' . $clean; } $sanitized = esc_url_raw( rtrim( $clean, '/' ) ); if ( empty( $sanitized ) || ! preg_match( '#^https://#i', $sanitized ) ) { return KRAWLUP_DEFAULT_ENDPOINT; } return $sanitized; } /** * Sanitize boolean checkbox fields. * * @param mixed $value Input value. * @return string '1' or '0'. */ public function sanitize_checkbox( $value ) { return ! empty( $value ) ? '1' : '0'; } /** * Render settings page template. */ public function render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'krawlup' ) ); } $token = get_option( 'krawlup_site_token', '' ); $custom_domain = get_option( 'krawlup_custom_domain', KRAWLUP_DEFAULT_ENDPOINT ); $track_admin = get_option( 'krawlup_track_admin', '0' ); $track_woocommerce = get_option( 'krawlup_track_woocommerce', '1' ); $has_wc = class_exists( 'WooCommerce' ); $is_connected = ! empty( $token ); ?>
K

' . esc_html__( 'Krawlup Dashboard', 'krawlup' ) . ' ↗' ); ?>