post_type !== 'bp_campaign' ) {
return '';
}
$meta = MetaBox::get_all( $campaign_id );
$stats = CampaignStats::get_stats( $campaign_id );
$template_key = $meta['bpc_template_key'] ?? '';
$all_templates = TemplateManager::get_all();
$theme_class = ( $template_key && isset( $all_templates[ $template_key ]['theme_class'] ) )
? ' ' . sanitize_html_class( $all_templates[ $template_key ]['theme_class'] )
: '';
if ( $is_preview && $preview_layout !== null ) {
$layout_data = $preview_layout;
} else {
$raw = $meta['bpc_fields_layout'] ?? [];
$layout_data = self::normalize_layout( $raw );
}
$columns = $layout_data['columns'] ?? [];
$layout = $layout_data['layout'] ?? '1-column';
// Only fall back to default when there are literally no columns.
// Blank templates (1-col, 2-col, 3-col) have columns with empty elements arrays —
// that is intentional and must render as blank to match the editor and preview.
if ( empty( $columns ) ) {
$layout_data = self::default_layout();
$columns = $layout_data['columns'];
$layout = $layout_data['layout'];
}
$color_style = self::generate_color_style( $meta, $campaign_id );
ob_start();
?>
block for campaign colour customisation.
*
* Only emits rules for colours that are explicitly set, so theme CSS remains
* the default when a colour is empty. Uses !important to override any theme class.
*
* @param array $meta Campaign meta (bpc_color_primary, background, …).
* @param int $campaign_id Scopes the selectors. 0 = isolated builder preview iframe.
* @return string or ''.
*/
private static function generate_color_style( array $meta, int $campaign_id ): string {
// Campaign background colour (set in the builder's Advanced tab). The
// per-button colour is now a property of the Donate Button widget
// (settings['button_color']), not a campaign-wide override.
$background = ! empty( $meta['bpc_color_background'] ) ? sanitize_hex_color( $meta['bpc_color_background'] ) : '';
if ( ! $background ) {
return '';
}
// $campaign_id is typed int and $background is validated hex — both safe for CSS output.
$scope = $campaign_id > 0
? '.bp-campaign[data-campaign-id="' . $campaign_id . '"]'
: '.bp-campaign';
$css = $scope . ' { background-color: ' . $background . ' !important; }';
return '';
}
/**
* Render a single element by type.
*
* @param array $element Element definition with type, settings.
* @param int $campaign_id
* @param \WP_Post $post
* @param array $meta Campaign meta from MetaBox::get_all().
* @param array $stats Campaign stats from CampaignStats::get_stats().
* @return string HTML output.
*/
public static function render_element(
array $element,
int $campaign_id,
\WP_Post $post,
array $meta,
array $stats
): string {
$type = $element['type'] ?? '';
$settings = $element['settings'] ?? [];
$el_id = $element['id'] ?? '';
ob_start();
switch ( $type ) {
case 'campaign_title':
$user_color = self::css_hex_color( $settings['color'] ?? '' );
$user_size = self::css_font_size( $settings['font_size'] ?? '' );
$title_text = ! empty( $settings['title'] ) ? $settings['title'] : $post->post_title;
$align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'left';
// A user-set value is emitted with !important so it wins over
// template rules (some templates force the title colour/size with
// !important). When unset we emit a plain default the template can
// still override.
$title_style = '' !== $user_color ? 'color:' . $user_color . ' !important;' : 'color:#1a1a2e;';
$title_style .= '' !== $user_size ? 'font-size:' . $user_size . ' !important;' : 'font-size:32px;';
$title_style .= self::decls_to_style( self::typography_common_decls( $settings ) );
$title_style .= 'text-align:' . $align . ';';
?>
post_content;
$desc_width = isset( $settings['width'] ) ? max( 10, min( 100, (int) $settings['width'] ) ) : 100;
$desc_align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'left';
if ( ! $desc_headline && ! $desc_content ) break;
// Wrapper carries layout only. Typography is applied directly to the
// headline and content elements (with !important) so it beats template
// rules that target those elements specifically. The headline (title)
// and the body each have their own independent typography set.
$desc_wrap = 'width:' . $desc_width . '%;';
if ( 'center' === $desc_align ) $desc_wrap .= 'margin:0 auto;';
elseif ( 'right' === $desc_align ) $desc_wrap .= 'margin-left:auto;';
// Title (headline) typography — its own set under the `title_`
// prefixed keys. font-size IS applied here so the title can be
// sized directly (empty leaves the template heading scale).
$title_typo = self::prefixed_typography( $settings, 'title_' );
$title_color = self::css_hex_color( $title_typo['color'] ?? '' );
$title_size = self::css_font_size( $title_typo['font_size'] ?? '' );
$desc_headline_style = self::decls_to_style( self::typography_common_decls( $title_typo ) );
if ( '' !== $title_color ) $desc_headline_style .= 'color:' . $title_color . ' !important;';
if ( '' !== $title_size ) $desc_headline_style .= 'font-size:' . $title_size . ' !important;';
// Body (content) typography — the base (unprefixed) keys, so any
// previously-saved description typography still applies here.
$desc_color = self::css_hex_color( $settings['color'] ?? '' );
$desc_size = self::css_font_size( $settings['font_size'] ?? '' );
$desc_content_style = self::decls_to_style( self::typography_common_decls( $settings ) );
if ( '' !== $desc_color ) $desc_content_style .= 'color:' . $desc_color . ' !important;';
if ( '' !== $desc_size ) $desc_content_style .= 'font-size:' . $desc_size . ' !important;';
?>
0 ) {
$img_data = wp_get_attachment_image_src( $src_id, $size );
$src = $img_data ? $img_data[0] : '';
}
if ( ! $src ) {
$src = ! empty( $settings['src'] ) ? $settings['src'] : get_the_post_thumbnail_url( $campaign_id, $size );
}
$alt_text = ! empty( $settings['alt'] ) ? $settings['alt'] : $post->post_title;
$ph_width = isset( $settings['width'] ) ? max( 10, min( 100, (int) $settings['width'] ) ) : 100;
$ph_align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'center';
if ( $src ) :
$img_style = 'width:auto;max-width:' . $ph_width . '%;height:auto;display:block;border-radius:4px;';
if ( 'center' === $ph_align ) $img_style .= 'margin:0 auto;';
elseif ( 'right' === $ph_align ) $img_style .= 'margin-left:auto;';
?>
'flex-start', 'center' => 'center', 'right' => 'flex-end' ];
$justify = $align_map[ $align ] ?? 'flex-start';
$currency_sym = self::currency_symbol( $currency );
// Progress label: ceiling (rounded up integer) when round_amounts, else 1 decimal place.
if ( $round_amounts ) {
$display_progress = (int) ceil( $goal > 0 ? min( 100, ( $raised / $goal ) * 100 ) : 0 );
$goal_fmt = number_format( (int) ceil( $goal ) );
} else {
$display_progress = $progress; // 1 decimal float from CampaignStats
$goal_fmt = number_format( $goal, 2 );
}
?>
0 ? min( 100, round( ( (float) $stats['total_raised'] / $goal ) * 100, 1 ) ) : 0;
$sm_currency = self::global_currency();
$sm_currency_sym = self::currency_symbol( $sm_currency );
$wrap_style = 'width:' . $sm_width . '%;';
if ( 'center' === $sm_align ) $wrap_style .= 'margin:0 auto;';
elseif ( 'right' === $sm_align ) $wrap_style .= 'margin-left:auto;';
?>
post_author;
$role_title = ! empty( $settings['role_title'] ) ? $settings['role_title'] : __( 'Organizer', 'better-payment' );
$description = ! empty( $settings['description'] ) ? $settings['description'] : '';
$width = isset( $settings['width'] ) ? max( 10, min( 100, (int) $settings['width'] ) ) : 100;
$align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true )
? $settings['align'] : 'left';
$creator = get_user_by( 'ID', $creator_user_id );
if ( ! $creator ) break;
$wrap_style = 'width:' . $width . '%;';
if ( 'center' === $align ) {
$wrap_style .= 'margin:0 auto;';
} elseif ( 'right' === $align ) {
$wrap_style .= 'margin-left:auto;';
}
?>
$a, 'is_default' => false ];
}
}
?>
post_title );
$sh_target = $sh_open_new_tab ? 'target="_blank" rel="noopener noreferrer"' : '';
$sh_justify = [ 'center' => 'center', 'right' => 'flex-end' ][ $sh_align ] ?? 'flex-start';
$share_links = [
'twitter' => 'https://twitter.com/intent/tweet?url=' . rawurlencode( $sh_page_url ) . '&text=' . $sh_title,
'facebook' => 'https://www.facebook.com/sharer/sharer.php?u=' . rawurlencode( $sh_page_url ),
'linkedin' => 'https://www.linkedin.com/shareArticle?mini=true&url=' . rawurlencode( $sh_page_url ) . '&title=' . $sh_title,
'pinterest' => 'https://pinterest.com/pin/create/button/?url=' . rawurlencode( $sh_page_url ) . '&description=' . $sh_title,
'mastodon' => 'https://mastodonshare.com/?text=' . $sh_title . '&url=' . rawurlencode( $sh_page_url ),
'threads' => 'https://threads.net/intent/post?text=' . $sh_title . '%20' . rawurlencode( $sh_page_url ),
'bluesky' => 'https://bsky.app/intent/compose?text=' . $sh_title . '%20' . rawurlencode( $sh_page_url ),
];
$active_sharing = array_filter( $share_links, function( $url, $key ) use ( $settings ) {
return ( $settings[ $key ] ?? true ) !== false;
}, ARRAY_FILTER_USE_BOTH );
if ( $active_sharing || $sh_headline ) :
?>
'center', 'right' => 'flex-end' ][ $sl_align ] ?? 'flex-start';
$all_link_keys = [ 'twitter', 'facebook', 'linkedin', 'instagram', 'tiktok', 'pinterest', 'youtube', 'threads', 'bluesky', 'mastodon' ];
$active_links = [];
foreach ( $all_link_keys as $key ) {
if ( ! empty( $settings[ $key ] ) ) {
$active_links[ $key ] = $settings[ $key ];
}
}
if ( $active_links || $sl_headline ) :
?>
' . $html . '';
}
/**
* Normalize raw stored layout to the column schema.
* Handles both old flat-array format and new column format.
*
* @param mixed $raw Value from MetaBox::get_all()['bpc_fields_layout'].
* @return array Normalized layout with 'layout' and 'columns' keys.
*/
public static function normalize_layout( $raw ): array {
if ( is_array( $raw ) && isset( $raw['columns'] ) ) {
return $raw;
}
// Legacy flat format: wrap all elements into a single column.
$elements = is_array( $raw ) ? $raw : [];
return [
'layout' => '1-column',
'columns' => [
[
'id' => 'main',
'label' => 'Main Content',
'width' => '100%',
'elements' => $elements,
],
],
];
}
/**
* Default layout used when no layout is saved (campaign has no fields yet).
*
* @return array
*/
private static function social_icon_svg( string $network ): string {
$paths = [
'twitter' => 'M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-4.714-6.231-5.401 6.231H2.744l7.73-8.835L1.254 2.25H8.08l4.253 5.622zm-1.161 17.52h1.833L7.084 4.126H5.117z',
'facebook' => 'M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z',
'linkedin' => 'M20.447 20.452h-3.554v-5.569c0-1.328-.027-3.037-1.852-3.037-1.853 0-2.136 1.445-2.136 2.939v5.667H9.351V9h3.414v1.561h.046c.477-.9 1.637-1.85 3.37-1.85 3.601 0 4.267 2.37 4.267 5.455v6.286zM5.337 7.433a2.062 2.062 0 01-2.063-2.065 2.064 2.064 0 112.063 2.065zm1.782 13.019H3.555V9h3.564v11.452zM22.225 0H1.771C.792 0 0 .774 0 1.729v20.542C0 23.227.792 24 1.771 24h20.451C23.2 24 24 23.227 24 22.271V1.729C24 .774 23.2 0 22.222 0h.003z',
'instagram' => 'M12 2.163c3.204 0 3.584.012 4.85.07 3.252.148 4.771 1.691 4.919 4.919.058 1.265.069 1.645.069 4.849 0 3.205-.012 3.584-.069 4.849-.149 3.225-1.664 4.771-4.919 4.919-1.266.058-1.644.07-4.85.07-3.204 0-3.584-.012-4.849-.07-3.26-.149-4.771-1.699-4.919-4.92-.058-1.265-.07-1.644-.07-4.849 0-3.204.013-3.583.07-4.849.149-3.227 1.664-4.771 4.919-4.919 1.266-.057 1.645-.069 4.849-.069zm0-2.163c-3.259 0-3.667.014-4.947.072-4.358.2-6.78 2.618-6.98 6.98-.059 1.281-.073 1.689-.073 4.948 0 3.259.014 3.668.072 4.948.2 4.358 2.618 6.78 6.98 6.98 1.281.058 1.689.072 4.948.072 3.259 0 3.668-.014 4.948-.072 4.354-.2 6.782-2.618 6.979-6.98.059-1.28.073-1.689.073-4.948 0-3.259-.014-3.667-.072-4.947-.196-4.354-2.617-6.78-6.979-6.98-1.281-.059-1.69-.073-4.949-.073zm0 5.838c-3.403 0-6.162 2.759-6.162 6.162S8.597 18.163 12 18.163s6.162-2.759 6.162-6.162S15.403 5.838 12 5.838zm0 10.162c-2.209 0-4-1.79-4-4 0-2.209 1.791-4 4-4s4 1.791 4 4c0 2.21-1.791 4-4 4zm6.406-11.845a1.44 1.44 0 100 2.881 1.44 1.44 0 000-2.881z',
'tiktok' => 'M12.525.02c1.31-.02 2.61-.01 3.91-.02.08 1.53.63 3.09 1.75 4.17 1.12 1.11 2.7 1.62 4.24 1.79v4.03c-1.44-.05-2.89-.35-4.2-.97-.57-.26-1.1-.59-1.62-.93-.01 2.92.01 5.84-.02 8.75-.08 1.4-.54 2.79-1.35 3.94-1.31 1.92-3.58 3.17-5.91 3.21-1.43.08-2.86-.31-4.08-1.03-2.02-1.19-3.44-3.37-3.65-5.71-.02-.5-.03-1-.01-1.49.18-1.9 1.12-3.72 2.58-4.96 1.66-1.44 3.98-2.13 6.15-1.72.02 1.48-.04 2.96-.04 4.44-.99-.32-2.15-.23-3.02.37-.63.41-1.11 1.04-1.36 1.75-.21.51-.15 1.07-.14 1.61.24 1.64 1.82 3.02 3.5 2.87 1.12-.01 2.19-.66 2.77-1.61.19-.33.4-.67.41-1.06.1-1.79.06-3.57.07-5.36.01-4.03-.01-8.05.02-12.07z',
'pinterest' => 'M12 0C5.373 0 0 5.372 0 12c0 5.084 3.163 9.426 7.627 11.174-.105-.949-.2-2.405.042-3.441.218-.937 1.407-5.965 1.407-5.965s-.359-.719-.359-1.782c0-1.668.967-2.914 2.171-2.914 1.023 0 1.518.769 1.518 1.69 0 1.029-.655 2.568-.994 3.995-.283 1.194.599 2.169 1.777 2.169 2.133 0 3.772-2.249 3.772-5.495 0-2.873-2.064-4.882-5.012-4.882-3.414 0-5.418 2.561-5.418 5.207 0 1.031.397 2.138.893 2.738a.36.36 0 01.083.345l-.333 1.36c-.053.22-.174.267-.402.161-1.499-.698-2.436-2.889-2.436-4.649 0-3.785 2.75-7.262 7.929-7.262 4.163 0 7.398 2.967 7.398 6.931 0 4.136-2.607 7.464-6.227 7.464-1.216 0-2.359-.632-2.75-1.378l-.748 2.853c-.271 1.043-1.002 2.35-1.492 3.146C9.57 23.812 10.763 24 12 24c6.627 0 12-5.373 12-12S18.627 0 12 0z',
'youtube' => 'M23.498 6.186a3.016 3.016 0 00-2.122-2.136C19.505 3.545 12 3.545 12 3.545s-7.505 0-9.377.505A3.017 3.017 0 00.502 6.186C0 8.07 0 12 0 12s0 3.93.502 5.814a3.016 3.016 0 002.122 2.136c1.871.505 9.376.505 9.376.505s7.505 0 9.377-.505a3.015 3.015 0 002.122-2.136C24 15.93 24 12 24 12s0-3.93-.502-5.814zM9.545 15.568V8.432L15.818 12l-6.273 3.568z',
'threads' => 'M12.186 24h-.007c-3.581-.024-6.334-1.205-8.184-3.509C2.35 18.44 1.5 15.586 1.472 12.01v-.017c.028-3.579.879-6.43 2.525-8.482C5.845 1.205 8.6.024 12.18 0h.014c2.746.02 5.043.725 6.826 2.098 1.677 1.29 2.858 3.13 3.509 5.467l-2.04.569c-1.104-3.96-3.898-5.984-8.304-6.015-2.91.022-5.11.936-6.54 2.717C4.307 6.504 3.616 8.914 3.589 12c.027 3.086.718 5.496 2.057 7.164 1.43 1.783 3.631 2.698 6.54 2.717 2.623-.02 4.358-.631 5.8-2.045 1.647-1.613 1.618-3.593 1.09-4.798-.31-.71-.873-1.3-1.634-1.75-.192 1.352-.622 2.446-1.284 3.272-.886 1.102-2.14 1.704-3.73 1.79-1.202.065-2.361-.218-3.259-.801-1.063-.689-1.685-1.74-1.752-2.964-.065-1.19.408-2.285 1.33-3.082.88-.76 2.119-1.207 3.583-1.291a13.853 13.853 0 013.271.165c-.07-.75-.273-1.318-.614-1.716-.434-.507-1.119-.768-2.036-.777h-.045c-.67 0-1.938.181-2.669 1.4l-1.812-.755c.97-1.868 2.836-2.677 4.484-2.677h.06c3.233.03 5.164 2.01 5.34 5.49.208 3.394-1.112 5.49-3.317 6.548-.384.186-.785.34-1.197.461C16.418 23.61 14.397 24 12.186 24z',
'bluesky' => 'M12 10.8c-1.087-2.114-4.046-6.053-6.798-7.995C2.566.944 1.561 1.266.902 1.565.139 1.908 0 3.08 0 3.768c0 .69.378 5.65.624 6.479.815 2.736 3.713 3.66 6.383 3.364.136-.02.275-.039.415-.056-.138.022-.276.04-.415.056-3.912.58-7.387 2.005-2.83 7.078 5.013 5.19 6.87-1.113 7.823-4.308.953 3.195 2.05 9.271 7.733 4.308 4.267-4.308 1.172-6.498-2.74-7.078a8.741 8.741 0 01-.415-.056c.14.017.279.036.415.056 2.67.297 5.568-.628 6.383-3.364.246-.828.624-5.79.624-6.478 0-.69-.139-1.861-.902-2.204-.659-.299-1.664-.62-4.3 1.24C16.046 4.748 13.087 8.687 12 10.8z',
'mastodon' => 'M23.268 5.313c-.35-2.578-2.617-4.61-5.304-5.004C17.51.242 15.792 0 11.813 0h-.03c-3.98 0-4.835.242-5.288.309C3.882.692 1.496 2.518.917 5.127.64 6.412.61 7.837.661 9.143c.074 1.874.088 3.745.26 5.611.118 1.24.325 2.47.62 3.68.55 2.237 2.777 4.098 4.96 4.857 2.336.792 4.849.923 7.256.38.265-.061.527-.132.786-.213.585-.184 1.27-.39 1.774-.753a.057.057 0 00.023-.043v-1.809a.052.052 0 00-.066-.051c-1.517.363-3.072.546-4.632.546-2.685 0-3.463-1.284-3.674-1.818a5.593 5.593 0 01-.319-1.433.053.053 0 01.066-.054c1.517.363 3.072.546 4.632.546.376 0 .75 0 1.124-.01 1.554-.043 3.19-.167 4.72-.498.038-.009.075-.015.11-.024 2.435-.464 4.753-1.92 4.989-5.604.005-.109.033-1.25.033-1.36 0-.43.138-3.032-.019-4.643zm-3.22 9.214h-2.058V9.47c0-1.063-.447-1.601-1.35-1.601-1 0-1.5.647-1.5 1.923v2.786h-2.048V9.792c0-1.276-.5-1.923-1.5-1.923-.903 0-1.35.538-1.35 1.601v5.04H7.884V9.32c0-1.062.27-1.907.81-2.534.558-.627 1.287-.948 2.192-.948 1.047 0 1.84.402 2.363 1.206l.509.855.51-.855c.523-.804 1.316-1.206 2.363-1.206.904 0 1.633.32 2.192.948.54.627.925 1.472.925 2.534v5.19z',
];
if ( ! isset( $paths[ $network ] ) ) {
return '';
}
return '';
}
/**
* Scale up inline px font-sizes in HTML content saved by the rich text editor.
* Adds 2px to every explicit pixel font-size so the text matches the campaign's
* base 16px scale (editor default is 14px).
*/
private static function scale_inline_font_sizes( string $html ): string {
return preg_replace_callback(
'/\bfont-size\s*:\s*(\d+(?:\.\d+)?)px/i',
function ( $m ) {
return 'font-size: ' . ( (float) $m[1] + 2 ) . 'px';
},
$html
);
}
/**
* Normalize a stored font-size setting into a safe CSS length.
* Numeric values are treated as pixels. Returns '' when unset/invalid.
*
* @param mixed $val
*/
private static function css_font_size( $val ): string {
if ( is_string( $val ) ) {
$val = trim( $val );
}
if ( '' === $val || null === $val ) {
return '';
}
if ( is_numeric( $val ) ) {
return ( (float) $val ) . 'px';
}
if ( is_string( $val ) && preg_match( '/^\d+(\.\d+)?(px|em|rem|%)$/', $val ) ) {
return $val;
}
return '';
}
/**
* Validate a stored font-family stack. Allows letters, numbers, spaces,
* commas, quotes and hyphens only — blocks CSS breakout. Returns '' when
* unset/invalid.
*
* @param mixed $val
*/
private static function css_font_family( $val ): string {
$val = is_string( $val ) ? trim( $val ) : '';
if ( '' === $val ) {
return '';
}
return preg_match( "/^[A-Za-z0-9 ,'\"_-]+$/", $val ) ? $val : '';
}
/**
* Validate a stored font-style value. Returns '' when unset/invalid.
*
* @param mixed $val
*/
private static function css_font_style( $val ): string {
$val = is_string( $val ) ? strtolower( trim( $val ) ) : '';
return in_array( $val, [ 'normal', 'italic', 'oblique' ], true ) ? $val : '';
}
/**
* Validate a stored hex color. Returns '' when unset/invalid.
*
* @param mixed $val
*/
private static function css_hex_color( $val ): string {
$val = is_string( $val ) ? trim( $val ) : '';
return preg_match( '/^#[0-9a-fA-F]{3,8}$/', $val ) ? $val : '';
}
/**
* Normalize a stored CSS length (letter/word spacing, line-height). Numeric
* values are treated as pixels; a leading minus is allowed. Returns '' when
* unset/invalid.
*
* @param mixed $val
*/
private static function css_length( $val ): string {
if ( is_string( $val ) ) {
$val = trim( $val );
}
if ( '' === $val || null === $val ) {
return '';
}
if ( is_numeric( $val ) ) {
return ( (float) $val ) . 'px';
}
if ( is_string( $val ) && preg_match( '/^-?\d+(\.\d+)?(px|em|rem|%)$/', $val ) ) {
return $val;
}
return '';
}
/**
* Validate a stored font-weight value. Returns '' when unset/invalid.
*
* @param mixed $val
*/
private static function css_font_weight( $val ): string {
if ( is_int( $val ) ) {
$val = (string) $val;
}
$val = is_string( $val ) ? strtolower( trim( $val ) ) : '';
$allowed = [ '100', '200', '300', '400', '500', '600', '700', '800', '900', 'normal', 'bold', 'bolder', 'lighter' ];
return in_array( $val, $allowed, true ) ? $val : '';
}
/**
* Validate a stored value against an allowlist of CSS keywords.
*
* @param mixed $val
* @param array $allowed
*/
private static function css_keyword( $val, array $allowed ): string {
$val = is_string( $val ) ? strtolower( trim( $val ) ) : '';
return in_array( $val, $allowed, true ) ? $val : '';
}
/**
* Extract a prefixed typography set (e.g. 'title_font_family') back into the
* base keys the typography helpers expect ('font_family'). Lets one element
* carry more than one independent typography set (see the Description widget,
* which styles its title and body separately) while reusing the same helpers.
*
* @param array $settings Element settings.
* @param string $prefix Key prefix to strip (e.g. 'title_').
* @return array Base-keyed typography settings.
*/
private static function prefixed_typography( array $settings, string $prefix ): array {
$keys = [
'font_family', 'font_size', 'font_weight', 'text_transform', 'font_style',
'text_decoration', 'line_height', 'letter_spacing', 'word_spacing', 'color',
];
$out = [];
foreach ( $keys as $key ) {
if ( isset( $settings[ $prefix . $key ] ) ) {
$out[ $key ] = $settings[ $prefix . $key ];
}
}
return $out;
}
/**
* Build the "common" typography CSS declarations (everything except color and
* font-size, which callers handle per-element) from element settings.
* Only user-set, valid values are included.
*
* @param array $settings
* @return array Map of css-property => value.
*/
private static function typography_common_decls( array $settings ): array {
$decls = [];
$family = self::css_font_family( $settings['font_family'] ?? '' );
if ( '' !== $family ) {
$decls['font-family'] = $family;
}
$weight = self::css_font_weight( $settings['font_weight'] ?? '' );
if ( '' !== $weight ) {
$decls['font-weight'] = $weight;
}
$style = self::css_font_style( $settings['font_style'] ?? '' );
if ( '' !== $style ) {
$decls['font-style'] = $style;
}
$transform = self::css_keyword( $settings['text_transform'] ?? '', [ 'uppercase', 'lowercase', 'capitalize', 'none' ] );
if ( '' !== $transform ) {
$decls['text-transform'] = $transform;
}
$decoration = self::css_keyword( $settings['text_decoration'] ?? '', [ 'underline', 'overline', 'line-through', 'none' ] );
if ( '' !== $decoration ) {
$decls['text-decoration'] = $decoration;
}
$line_height = self::css_length( $settings['line_height'] ?? '' );
if ( '' !== $line_height ) {
$decls['line-height'] = $line_height;
}
$letter = self::css_length( $settings['letter_spacing'] ?? '' );
if ( '' !== $letter ) {
$decls['letter-spacing'] = $letter;
}
$word = self::css_length( $settings['word_spacing'] ?? '' );
if ( '' !== $word ) {
$decls['word-spacing'] = $word;
}
return $decls;
}
/**
* Flatten a declaration map into an inline-style string. Every declaration is
* emitted with !important so user overrides win over template stylesheet rules.
*
* @param array $decls
*/
private static function decls_to_style( array $decls ): string {
$out = '';
foreach ( $decls as $prop => $value ) {
$out .= $prop . ':' . $value . ' !important;';
}
return $out;
}
/**
* Returns the global Better Payment currency code from plugin settings.
*/
private static function global_currency(): string {
$code = DB::get_settings( 'better_payment_settings_general_general_currency' );
return ( is_string( $code ) && $code !== '' ) ? $code : 'USD';
}
private static function currency_symbol( string $code ): string {
$map = [
'USD' => '$', 'EUR' => '€', 'GBP' => '£', 'JPY' => '¥',
'CAD' => 'CA$', 'AUD' => 'A$', 'INR' => '₹', 'BRL' => 'R$',
'MXN' => 'MX$', 'SGD' => 'S$', 'CHF' => 'CHF', 'SEK' => 'kr',
'NOK' => 'kr', 'DKK' => 'kr', 'NZD' => 'NZ$', 'ZAR' => 'R',
'BDT' => '৳', 'PKR' => '₨', 'NGN' => '₦', 'KES' => 'KSh',
];
return $map[ strtoupper( $code ) ] ?? $code;
}
/**
* Render a template definition as HTML for the picker iframe preview.
*
* @param string $key Template key from TemplateManager.
* @return string HTML fragment, or empty string if key not found.
*/
public static function render_template_preview( string $key ): string {
$templates = TemplateManager::get_all();
if ( ! isset( $templates[ $key ] ) ) {
return '';
}
$template = $templates[ $key ];
$columns = $template['columns'] ?? [];
$layout = $template['layout'] ?? '1-column';
$first_users = get_users( [ 'fields' => [ 'ID' ], 'number' => 1 ] );
$first_creator_id = ! empty( $first_users ) ? (int) $first_users[0]->ID : get_current_user_id();
$fake_post = new \WP_Post( (object) [
'ID' => 0,
'post_title' => $template['default_title'] ?? $template['label'] ?? 'Campaign Preview',
'post_content' => '',
'post_author' => $first_creator_id,
'post_type' => 'bp_campaign',
'post_status' => 'publish',
'post_name' => $key,
] );
$meta = [
'bpc_goal_amount' => 10000,
'bpc_color_primary' => $template['preview_color'] ?? '#6b63f6',
'bpc_color_background' => '',
'bpc_suggested_amounts' => [],
'bpc_allow_custom_amount' => true,
'bpc_minimum_amount' => '',
'bpc_form_page_id' => 0,
'bpc_status' => 'active',
'bpc_template_key' => $key,
'bpc_css_class' => '',
];
$stats = [
'total_raised' => 3750,
'progress' => 37.5,
'donor_count' => 42,
'days_remaining' => 18,
];
$theme_class = isset( $template['theme_class'] ) ? ' ' . sanitize_html_class( $template['theme_class'] ) : '';
ob_start();
?>
so the JS overlay
* can measure positions and wire up hover/drag interactions. Works for both
* new unsaved campaigns (campaign_id = 0) and existing ones.
*
* @param array $layout_data Builder layout: { layout, columns }.
* @param array $meta_input Campaign meta from the builder store (includes 'title').
* @param int $campaign_id 0 for new campaigns; real ID to pull live stats.
* @return string Full HTML document string.
*/
public static function build_preview_document(
array $layout_data,
array $meta_input,
int $campaign_id = 0
): string {
// ── Post + stats ──────────────────────────────────────────────────────
$post = null;
$stats = null;
if ( $campaign_id > 0 ) {
$real = get_post( $campaign_id );
if ( $real && $real->post_type === 'bp_campaign' ) {
$post = $real;
$stats = CampaignStats::get_stats( $campaign_id );
}
}
if ( ! $post ) {
$post = new \WP_Post( (object) [
'ID' => 0,
'post_title' => sanitize_text_field( $meta_input['title'] ?? 'Campaign Preview' ),
'post_content' => '',
'post_author' => get_current_user_id(),
'post_type' => 'bp_campaign',
'post_status' => 'publish',
'post_name' => 'preview',
] );
$stats = [ 'total_raised' => 0, 'progress' => 0, 'donor_count' => 0, 'days_remaining' => null ];
} else {
// Always reflect the current editor title, even for saved campaigns.
$override = sanitize_text_field( $meta_input['title'] ?? '' );
if ( $override !== '' ) {
$post->post_title = $override;
}
}
// Override stats fields that depend on unsaved builder meta so the preview
// reflects the current editor values without requiring a save first.
$preview_end_date = $meta_input['bpc_end_date'] ?? '';
if ( $preview_end_date !== '' ) {
$diff = strtotime( $preview_end_date ) - current_time( 'timestamp' );
$stats['days_remaining'] = max( 0, (int) ceil( $diff / DAY_IN_SECONDS ) );
} else {
$stats['days_remaining'] = null;
}
$preview_goal = (float) ( $meta_input['bpc_goal_amount'] ?? 0 );
if ( $preview_goal > 0 ) {
$stats['progress'] = min( 100.0, round( ( $stats['total_raised'] / $preview_goal ) * 100, 1 ) );
}
// ── Meta ─────────────────────────────────────────────────────────────
$meta_defaults = [
'bpc_goal_amount' => 0,
'bpc_color_primary' => '#6b63f6',
'bpc_color_background' => '',
'bpc_suggested_amounts' => [],
'bpc_allow_custom_amount' => true,
'bpc_minimum_amount' => '',
'bpc_form_page_id' => 0,
'bpc_status' => 'active',
'bpc_template_key' => '',
'bpc_css_class' => '',
];
if ( $campaign_id > 0 ) {
$meta = array_merge( MetaBox::get_all( $campaign_id ), $meta_defaults, $meta_input );
} else {
$meta = array_merge( $meta_defaults, $meta_input );
}
// ── Layout ───────────────────────────────────────────────────────────
$columns = $layout_data['columns'] ?? [];
$layout = $layout_data['layout'] ?? '1-column';
if ( empty( $columns ) ) {
$default = self::default_layout();
$columns = $default['columns'];
$layout = $default['layout'];
}
// ── Template theme class ──────────────────────────────────────────────
$template_key = $meta['bpc_template_key'] ?? '';
$all_templates = TemplateManager::get_all();
$theme_class = ( $template_key && isset( $all_templates[ $template_key ]['theme_class'] ) )
? ' ' . sanitize_html_class( $all_templates[ $template_key ]['theme_class'] )
: '';
// ── Campaign HTML with element-id wrappers ────────────────────────────
$color_style = self::generate_color_style( $meta, $campaign_id );
ob_start();
?>
' . "\n"
: '';
return '' . "\n"
. '' . "\n"
. '' . "\n"
. '' . "\n"
. '' . "\n"
. '' . "\n"
. '' . "\n"
. $extra_link
. '' . "\n"
. '' . "\n"
. '' . "\n"
. preg_replace( '/