post_type ) {
return;
}
$graph = $this->decode_meta( $id, FunnelCpt::META_GRAPH_DRAFT );
if ( !$graph || empty( $graph['nodes'] ) ) {
$graph = $this->decode_meta( $id, FunnelCpt::META_GRAPH );
}
if ( !$graph || empty( $graph['nodes'] ) ) {
wp_die( esc_html__( 'This funnel has no steps yet.', 'funnelnode' ) );
}
$settings = ( $this->decode_meta( $id, FunnelCpt::META_SETTINGS ) ?: [] );
show_admin_bar( false );
$this->enqueue_assets();
$this->print_config(
$id,
$graph,
$settings,
true
);
wp_add_inline_style( 'funnelnode-renderer', 'body.funnelnode-preview{margin:0;min-height:100vh;background:#f4f6fa;display:grid;' . 'align-items:start;padding:40px 16px 64px;box-sizing:border-box;}' );
?>
>
post_title . ' — ' . __( 'Preview', 'funnelnode' ) );
?>
0,
], (array) $atts, self::SHORTCODE );
$id = absint( $atts['id'] );
if ( !$id ) {
return '';
}
$post = get_post( $id );
if ( !$post instanceof \WP_Post || FunnelCpt::POST_TYPE !== $post->post_type ) {
return '';
}
$is_published = 'publish' === $post->post_status;
$can_preview = current_user_can( Plugin::capability() );
if ( !$is_published && !$can_preview ) {
return '';
}
$graph_key = ( $is_published ? FunnelCpt::META_GRAPH : FunnelCpt::META_GRAPH_DRAFT );
$graph = $this->decode_meta( $id, $graph_key );
if ( !$graph || empty( $graph['nodes'] ) ) {
return '';
}
$settings = ( $this->decode_meta( $id, FunnelCpt::META_SETTINGS ) ?: [] );
$this->enqueue_assets();
$this->print_config( $id, $graph, $settings );
$notice = ( $is_published ? '' : $this->render_draft_notice( $id ) );
return $notice . sprintf( '', $id );
}
private function render_draft_notice( int $id ) : string {
$link = ' ' . esc_html__( 'Open in builder', 'funnelnode' ) . '';
return '' . esc_html__( 'Draft — this funnel is not published yet. Submissions are only saved and notification emails only sent once it is published.', 'funnelnode' ) . $link . ' ' . esc_html__( '(Only shown to logged-in editors.)', 'funnelnode' ) . '' . '
';
}
private function enqueue_assets() : void {
$dir = Edition::assets_subdir();
$js = FUNNELNODE_DIR . 'assets/dist/' . $dir . 'renderer.js';
$css = FUNNELNODE_DIR . 'assets/dist/' . $dir . 'renderer.css';
if ( !is_readable( $js ) ) {
return;
}
wp_enqueue_script(
'funnelnode-renderer',
FUNNELNODE_ASSETS_URL . $dir . 'renderer.js',
[],
(string) filemtime( $js ),
true
);
if ( is_readable( $css ) ) {
wp_enqueue_style(
'funnelnode-renderer',
FUNNELNODE_ASSETS_URL . $dir . 'renderer.css',
[],
(string) filemtime( $css )
);
$this->apply_branding();
}
}
private function apply_branding() : void {
$branding = Settings::branding();
[$vars, $card_vars] = $this->color_vars( $branding );
if ( !empty( $branding['button_uppercase'] ) ) {
$vars[] = '--wpfb-button-transform: uppercase;';
$vars[] = '--wpfb-button-letter-spacing: 0.04em;';
}
$shadow = (string) ($branding['card_shadow'] ?? 'soft');
if ( 'none' === $shadow ) {
$vars[] = '--wpfb-card-shadow: none;';
} elseif ( 'strong' === $shadow ) {
$vars[] = '--wpfb-card-shadow: 0 12px 40px rgba(17, 24, 39, 0.16);';
}
if ( 'theme' === (string) ($branding['font_source'] ?? 'system') ) {
$vars[] = '--wpfb-font-family-base: inherit;';
$vars[] = '--wpfb-font-family-heading: inherit;';
}
foreach ( $this->font_size_vars( $branding ) as $var ) {
$vars[] = $var;
}
$css = '';
if ( $vars ) {
$css .= '.wpfb-funnel{' . implode( '', $vars ) . '}';
}
if ( $card_vars ) {
$css .= '.wpfb-funnel .wpfb-card{' . implode( '', $card_vars ) . '}';
}
if ( '' !== $css ) {
wp_add_inline_style( 'funnelnode-renderer', $css );
}
}
private function font_size_vars( array $branding ) : array {
$map = [
'heading' => '--wpfb-text-h2',
'body' => '--wpfb-text-base',
'button' => '--wpfb-button-font-size',
'checkbox' => '--wpfb-checkbox-font-size',
];
$sizes = (array) ($branding['font_sizes'] ?? []);
$vars = [];
foreach ( $map as $key => $token ) {
$min = (int) ($sizes[$key]['min'] ?? 0);
$max = (int) ($sizes[$key]['max'] ?? 0);
if ( $min < 8 || $min > 120 || $max < 8 || $max > 120 ) {
continue;
}
if ( $min > $max ) {
[$min, $max] = [$max, $min];
}
if ( $min === $max ) {
$vars[] = sprintf( '%s: %dpx;', $token, $min );
continue;
}
$slope = ($max - $min) / (1280 - 360);
$intercept = $min - $slope * 360;
$vars[] = sprintf(
'%s: clamp(%dpx, %.3fpx + %.3fvw, %dpx);',
$token,
$min,
$intercept,
$slope * 100,
$max
);
}
return $vars;
}
private function color_vars( array $branding ) : array {
$vars = $this->theme_vars( (string) ($branding['primary_color'] ?? ''), (string) ($branding['button_radius'] ?? '') );
$button_text = (string) ($branding['button_text_color'] ?? '');
if ( $this->is_color( $button_text ) ) {
$vars[] = '--wpfb-color-button-text: ' . $button_text . ';';
}
$card = (string) ($branding['card_background'] ?? '');
$is_dark = false;
if ( $this->is_color( $card ) ) {
$vars[] = '--wpfb-color-surface: ' . $card . ';';
$is_dark = $this->is_dark( $card );
}
$card_vars = [];
$text = (string) ($branding['text_color'] ?? '');
if ( $this->is_color( $text ) ) {
$card_vars[] = '--wpfb-color-text: ' . $text . ';';
$card_vars[] = '--wpfb-color-text-muted: ' . $text . ';';
} elseif ( $is_dark ) {
$card_vars[] = '--wpfb-color-text: #f9fafb;';
$card_vars[] = '--wpfb-color-text-muted: color-mix(in srgb, #f9fafb 62%, var(--wpfb-color-surface));';
}
if ( $is_dark ) {
$card_vars[] = '--wpfb-color-border: rgba(255, 255, 255, 0.18);';
}
return [$vars, $card_vars];
}
private function theme_vars( string $color, string $radius ) : array {
$vars = [];
if ( $this->is_color( $color ) ) {
$vars[] = '--wpfb-color-primary: ' . $color . ';';
$vars[] = '--wpfb-color-primary-hover: color-mix(in srgb, ' . $color . ' 85%, #000);';
$vars[] = '--wpfb-focus-ring-color: ' . $color . ';';
}
if ( preg_match( '/^\\d{1,3}(px|rem|em|%)?$/', $radius ) ) {
$vars[] = '--wpfb-button-radius: ' . $radius . (( is_numeric( $radius ) ? 'px' : '' )) . ';';
}
return $vars;
}
private function is_color( string $value ) : bool {
return (bool) preg_match( '/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value );
}
private function is_dark( string $hex ) : bool {
$h = ltrim( $hex, '#' );
if ( 3 === strlen( $h ) ) {
$h = $h[0] . $h[0] . $h[1] . $h[1] . $h[2] . $h[2];
}
$lin = static function ( int $v ) : float {
$c = $v / 255;
return ( $c <= 0.04045 ? $c / 12.92 : pow( ($c + 0.055) / 1.055, 2.4 ) );
};
$lum = 0.2126 * $lin( (int) hexdec( substr( $h, 0, 2 ) ) ) + 0.7151999999999999 * $lin( (int) hexdec( substr( $h, 2, 2 ) ) ) + 0.0722 * $lin( (int) hexdec( substr( $h, 4, 2 ) ) );
return $lum < 0.4;
}
private function print_config(
int $id,
array $graph,
array $settings,
bool $preview = false
) : void {
if ( isset( $this->printed[$id] ) ) {
return;
}
$this->printed[$id] = true;
$global_spam = (array) (Settings::all()['spam'] ?? []);
$config = [
'id' => $id,
'title' => wp_specialchars_decode( (string) get_the_title( $id ), ENT_QUOTES ),
'graph' => $graph,
'settings' => [
'i18n' => $settings['i18n'] ?? [],
'privacy' => [
'consent_text' => ( '' !== trim( (string) ($settings['privacy']['consent_text'] ?? '') ) ? (string) $settings['privacy']['consent_text'] : (string) (Settings::all()['privacy']['consent_text'] ?? '') ),
'privacy_url' => ( get_privacy_policy_url() ?: $settings['privacy']['privacy_url'] ?? '' ),
],
'redirect_url' => $settings['redirect_url'] ?? '',
'attention_nudge' => false !== ($settings['attention_nudge'] ?? true),
'progress' => ( in_array( $settings['progress'] ?? '', [
'count',
'bar',
'count_from_2',
'off'
], true ) ? (string) $settings['progress'] : 'count' ),
'theme' => [
'progress_style' => (string) (Settings::branding()['progress_style'] ?? 'bar'),
],
'spam' => [
'honeypot' => !empty( $global_spam['honeypot'] ),
],
],
'apiBase' => esc_url_raw( rest_url( Api::NAMESPACE ) ),
];
if ( $preview ) {
$config['preview'] = true;
}
wp_add_inline_script( 'funnelnode-renderer', sprintf( 'window.funnelnodeFunnels = window.funnelnodeFunnels || {}; window.funnelnodeFunnels[%d] = %s;', $id, wp_json_encode( $config ) ), 'before' );
}
private function decode_meta( int $id, string $key ) : array {
$raw = (string) get_post_meta( $id, $key, true );
if ( '' === $raw ) {
return [];
}
$decoded = json_decode( $raw, true );
return ( is_array( $decoded ) ? $decoded : [] );
}
}