< function tfk_mc_get_prefill_email( $api ) { // Prefer signed token via mcid $mcid = isset( $_GET['mcid'] ) ? sanitize_text_field( wp_unslash( $_GET['mcid'] ) ) : ''; if ( $mcid && is_object( $api ) && method_exists( $api, 'decode_signed_token' ) ) { $email = $api->decode_signed_token( $mcid ); if ( $email ) { return $email; } } // Fallback: prefill email param (convenience only) $email_qs = isset( $_GET['email'] ) ? sanitize_email( wp_unslash( $_GET['email'] ) ) : ''; return is_email( $email_qs ) ? $email_qs : ''; } ?php if ( ! defined( 'ABSPATH' ) ) { exit; } /** * View renderer (MVC-light). */ function tfk_mc_render( $template, $data = array() ) { $file = TFK_MC_PLUGIN_PATH . 'templates/' . $template . '.php'; if ( ! file_exists( $file ) ) { return ''; } if ( is_array( $data ) ) { extract( $data, EXTR_SKIP ); } ob_start(); include $file; return ob_get_clean(); } function tfk_mc_notice_html( $type, $message ) { return tfk_mc_render( 'notice', array( 'type' => $type, 'message' => $message, ) ); } function tfk_mc_get_token_param_name() { return defined( 'TFK_MC_TOKEN_PARAM' ) && TFK_MC_TOKEN_PARAM ? (string) TFK_MC_TOKEN_PARAM : 'mcid'; } function tfk_mc_get_profile_url() { $path = defined( 'TFK_MC_PROFILE_PATH' ) ? (string) TFK_MC_PROFILE_PATH : '/email-marketing/profil/'; return site_url( $path ); } function tfk_mc_get_unsub_url() { $path = defined( 'TFK_MC_UNSUB_PATH' ) ? (string) TFK_MC_UNSUB_PATH : '/email-marketing/afmeld/'; return site_url( $path ); } function tfk_mc_mask_email( $email ) { $email = (string) $email; if ( ! is_email( $email ) ) { return ''; } $parts = explode( '@', $email ); if ( count( $parts ) !== 2 ) { return $email; } $name = $parts[0]; $domain = $parts[1]; $masked = substr( $name, 0, 1 ) . str_repeat( '*', max( 1, strlen( $name ) - 2 ) ) . substr( $name, -1 ); return $masked . '@' . $domain; } /** * Resolve email context from token/email GET params. * Returns array(email, email_locked, token) */ function tfk_mc_resolve_identity( TFK_MC_API $api ) { $token_param = tfk_mc_get_token_param_name(); $token = isset( $_GET[ $token_param ] ) ? sanitize_text_field( wp_unslash( $_GET[ $token_param ] ) ) : ''; $email = ''; $email_locked = false; if ( $token ) { $email = $api->find_email_by_token( $token ); if ( $email ) { $email_locked = true; } } if ( ! $email ) { $email = isset( $_GET['email'] ) ? sanitize_email( wp_unslash( $_GET['email'] ) ) : ''; if ( is_email( $email ) ) { $email_locked = false; } else { $email = ''; } } return array( $email, $email_locked, $token ); } /** * Shortcode: [TFK_MC_UPDATE] */ function tfk_mc_shortcode_update( $atts ) { $api = new TFK_MC_API(); $configured = $api->is_configured(); $notice_html = ''; list( $email_context, $email_locked, $token ) = tfk_mc_resolve_identity( $api ); // Handle POST $posted = ( isset( $_POST['tfk_mc_action'] ) && $_POST['tfk_mc_action'] === 'update' ); $values = array( 'email' => $email_context, 'fname' => '', 'lname' => '', 'bday' => '', ); if ( $posted && $configured ) { if ( ! isset( $_POST['tfk_mc_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['tfk_mc_nonce'] ) ), 'tfk_mc_update' ) ) { $notice_html = tfk_mc_notice_html( 'error', __( 'Invalid request. Please try again.', 'uncode-child' ) ); } else { $email_posted = isset( $_POST['tfk_mc_email'] ) ? sanitize_email( wp_unslash( $_POST['tfk_mc_email'] ) ) : ''; $email = $email_locked ? $email_context : $email_posted; $values['email'] = $email; $values['fname'] = isset( $_POST['tfk_mc_fname'] ) ? sanitize_text_field( wp_unslash( $_POST['tfk_mc_fname'] ) ) : ''; $values['lname'] = isset( $_POST['tfk_mc_lname'] ) ? sanitize_text_field( wp_unslash( $_POST['tfk_mc_lname'] ) ) : ''; $values['bday'] = isset( $_POST['tfk_mc_bday'] ) ? sanitize_text_field( wp_unslash( $_POST['tfk_mc_bday'] ) ) : ''; if ( ! is_email( $email ) ) { $notice_html = tfk_mc_notice_html( 'error', __( 'Please enter a valid email address.', 'uncode-child' ) ); } else { // Build merge fields $merge = array(); if ( defined( 'TFK_MC_MERGE_FNAME' ) && TFK_MC_MERGE_FNAME ) { $merge[ TFK_MC_MERGE_FNAME ] = $values['fname']; } if ( defined( 'TFK_MC_MERGE_LNAME' ) && TFK_MC_MERGE_LNAME ) { $merge[ TFK_MC_MERGE_LNAME ] = $values['lname']; } if ( defined( 'TFK_MC_MERGE_BDAY' ) && TFK_MC_MERGE_BDAY ) { // Accept MM/DD or MM-DD, normalize to MM/DD if valid $bday = trim( $values['bday'] ); $bday = str_replace( '-', '/', $bday ); if ( preg_match( '/^(0?[1-9]|1[0-2])\/(0?[1-9]|[12][0-9]|3[01])$/', $bday ) ) { $parts = explode( '/', $bday ); $merge[ TFK_MC_MERGE_BDAY ] = sprintf( '%02d/%02d', (int) $parts[0], (int) $parts[1] ); } } // Ensure token exists (so links can use ?mcid=... going forward) if ( defined( 'TFK_MC_MERGE_TOKEN' ) && TFK_MC_MERGE_TOKEN ) { $token = $api->get_or_create_token_for_email( $email ); if ( $token ) { $merge[ TFK_MC_MERGE_TOKEN ] = $token; } } $resp = $api->upsert_member( $email, $merge ); if ( is_wp_error( $resp ) ) { $notice_html = tfk_mc_notice_html( 'error', __( 'Could not update your details.', 'uncode-child' ) . ' ' . $resp->get_error_message() ); } else { $notice_html = tfk_mc_notice_html( 'success', __( 'Your details have been updated.', 'uncode-child' ) ); } } } } else { // If we have identity from token, prefill names from Mailchimp (best effort) if ( $configured && $email_context ) { $m = $api->get_member( $email_context ); if ( ! is_wp_error( $m ) && is_array( $m ) && isset( $m['data']['merge_fields'] ) && is_array( $m['data']['merge_fields'] ) ) { $mf = $m['data']['merge_fields']; if ( defined( 'TFK_MC_MERGE_FNAME' ) && TFK_MC_MERGE_FNAME && isset( $mf[ TFK_MC_MERGE_FNAME ] ) ) { $values['fname'] = (string) $mf[ TFK_MC_MERGE_FNAME ]; } if ( defined( 'TFK_MC_MERGE_LNAME' ) && TFK_MC_MERGE_LNAME && isset( $mf[ TFK_MC_MERGE_LNAME ] ) ) { $values['lname'] = (string) $mf[ TFK_MC_MERGE_LNAME ]; } } } } // Config notice if ( ! $configured ) { $notice_html = tfk_mc_notice_html( 'error', __( 'Mailchimp is not configured yet. Please fill in includes/tfk-mc-config.php.', 'uncode-child' ) ); } // Build unsubscribe URL with best available prefill $token_param = tfk_mc_get_token_param_name(); $unsub_url = tfk_mc_get_unsub_url(); $args = array(); if ( $token ) { $args[ $token_param ] = $token; } elseif ( $values['email'] ) { $args['email'] = $values['email']; } if ( ! empty( $args ) ) { $unsub_url = add_query_arg( $args, $unsub_url ); } return tfk_mc_render( 'update-form', array( 'configured' => $configured, 'notice_html' => $notice_html, 'values' => $values, 'email_locked' => $email_locked, 'token' => $token, 'unsub_url' => $unsub_url, 'token_param' => $token_param, ) ); } add_shortcode( 'TFK_MC_UPDATE', 'tfk_mc_shortcode_update' ); /** * Shortcode: [TFK_MC_UNS] */ function tfk_mc_shortcode_unsubscribe( $atts ) { $api = new TFK_MC_API(); $configured = $api->is_configured(); $notice_html = ''; list( $email_context, $email_locked, $token ) = tfk_mc_resolve_identity( $api ); $posted = ( isset( $_POST['tfk_mc_action'] ) && $_POST['tfk_mc_action'] === 'unsubscribe' ); $values = array( 'email' => $email_context, ); if ( $posted && $configured ) { if ( ! isset( $_POST['tfk_mc_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['tfk_mc_nonce'] ) ), 'tfk_mc_unsubscribe' ) ) { $notice_html = tfk_mc_notice_html( 'error', __( 'Invalid request. Please try again.', 'uncode-child' ) ); } else { $email_posted = isset( $_POST['tfk_mc_email'] ) ? sanitize_email( wp_unslash( $_POST['tfk_mc_email'] ) ) : ''; $email = $email_locked ? $email_context : $email_posted; $values['email'] = $email; if ( ! is_email( $email ) ) { $notice_html = tfk_mc_notice_html( 'error', __( 'Please enter a valid email address.', 'uncode-child' ) ); } else { $resp = $api->unsubscribe( $email ); if ( is_wp_error( $resp ) ) { $notice_html = tfk_mc_notice_html( 'error', __( 'Could not unsubscribe you.', 'uncode-child' ) . ' ' . $resp->get_error_message() ); } else { $notice_html = tfk_mc_notice_html( 'success', __( 'You have been unsubscribed.', 'uncode-child' ) ); } } } } if ( ! $configured ) { $notice_html = tfk_mc_notice_html( 'error', __( 'Mailchimp is not configured yet. Please fill in includes/tfk-mc-config.php.', 'uncode-child' ) ); } return tfk_mc_render( 'unsubscribe-form', array( 'configured' => $configured, 'notice_html' => $notice_html, 'values' => $values, 'email_locked' => $email_locked, 'masked_email' => $values['email'] ? tfk_mc_mask_email( $values['email'] ) : '', ) ); } add_shortcode( 'TFK_MC_UNS', 'tfk_mc_shortcode_unsubscribe' ); https://www.tfkenn.dk/page-sitemap.xml 2025-12-26T22:58:34+00:00