| @@ -5,8 +5,11 @@ | ||
| 5 | 5 | use Better_Payment\Lite\Admin\DB; |
| 6 | 6 | use Better_Payment\Lite\Campaign\CampaignStats; |
| 7 | 7 | use Better_Payment\Lite\Campaign\MetaBox; |
| 8 | 8 | use Better_Payment\Lite\Campaign\Templates\TemplateManager; |
| 9 | +use Better_Payment\Lite\Campaign\Support\Money; | |
| 10 | +use Better_Payment\Lite\Campaign\Elements\ElementRegistry; | |
| 11 | +use Better_Payment\Lite\Campaign\Elements\ProElementPreview; | |
| 9 | 12 | |
| 10 | 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | 14 | exit; |
| 12 | 15 | } |
| @@ -107,25 +110,28 @@ | ||
| 107 | 110 | * |
| 108 | 111 | * Only emits rules for colours that are explicitly set, so theme CSS remains |
| 109 | 112 | * the default when a colour is empty. Uses !important to override any theme class. |
| 110 | 113 | * |
| 111 | - * @param array $meta Campaign meta (bpc_color_primary, secondary, tertiary, button). | |
| 114 | + * @param array $meta Campaign meta (bpc_color_primary, background, …). | |
| 112 | 115 | * @param int $campaign_id Scopes the selectors. 0 = isolated builder preview iframe. |
| 113 | 116 | * @return string <style>…</style> or ''. |
| 114 | 117 | */ |
| 115 | 118 | private static function generate_color_style( array $meta, int $campaign_id ): string { |
| 116 | - $button = ! empty( $meta['bpc_color_button'] ) ? sanitize_hex_color( $meta['bpc_color_button'] ) : ''; | |
| 119 | + // Campaign background colour (set in the builder's Advanced tab). The | |
| 120 | + // per-button colour is now a property of the Donate Button widget | |
| 121 | + // (settings['button_color']), not a campaign-wide override. | |
| 122 | + $background = ! empty( $meta['bpc_color_background'] ) ? sanitize_hex_color( $meta['bpc_color_background'] ) : ''; | |
| 117 | 123 | |
| 118 | - if ( ! $button ) { | |
| 124 | + if ( ! $background ) { | |
| 119 | 125 | return ''; |
| 120 | 126 | } |
| 121 | 127 | |
| 122 | - // $campaign_id is typed int and $button is validated hex — both safe for CSS output. | |
| 128 | + // $campaign_id is typed int and $background is validated hex — both safe for CSS output. | |
| 123 | 129 | $scope = $campaign_id > 0 |
| 124 | 130 | ? '.bp-campaign[data-campaign-id="' . $campaign_id . '"]' |
| 125 | 131 | : '.bp-campaign'; |
| 126 | 132 | |
| 127 | - $css = $scope . ' .bp-donate_btn { background-color: ' . $button . ' !important; }'; | |
| 133 | + $css = $scope . ' { background-color: ' . $background . ' !important; }'; | |
| 128 | 134 | |
| 129 | 135 | return '<style>' . wp_strip_all_tags( $css ) . '</style>'; |
| 130 | 136 | } |
| 131 | 137 | |
| @@ -136,8 +142,15 @@ | ||
| 136 | 142 | * @param int $campaign_id |
| 137 | 143 | * @param \WP_Post $post |
| 138 | 144 | * @param array $meta Campaign meta from MetaBox::get_all(). |
| 139 | 145 | * @param array $stats Campaign stats from CampaignStats::get_stats(). |
| 146 | + * @param bool $is_preview True when rendering for the builder canvas or the | |
| 147 | + * template picker rather than a live campaign page. | |
| 148 | + * Passed through to add-on elements as `$ctx['is_preview']` | |
| 149 | + * so they can stand in sample content — data-driven | |
| 150 | + * elements otherwise render an empty state in the | |
| 151 | + * builder, leaving their settings with no visible | |
| 152 | + * effect. Never true on the frontend. | |
| 140 | 153 | * @return string HTML output. |
| 141 | 154 | */ |
| 142 | 155 | public static function render_element( |
| 143 | 156 | array $element, |
| @@ -143,9 +156,10 @@ | ||
| 143 | 156 | array $element, |
| 144 | 157 | int $campaign_id, |
| 145 | 158 | \WP_Post $post, |
| 146 | 159 | array $meta, |
| 147 | - array $stats | |
| 160 | + array $stats, | |
| 161 | + bool $is_preview = false | |
| 148 | 162 | ): string { |
| 149 | 163 | $type = $element['type'] ?? ''; |
| 150 | 164 | $settings = $element['settings'] ?? []; |
| 151 | 165 | $el_id = $element['id'] ?? ''; |
| @@ -154,15 +168,27 @@ | ||
| 154 | 168 | |
| 155 | 169 | switch ( $type ) { |
| 156 | 170 | |
| 157 | 171 | case 'campaign_title': |
| 158 | - $color = ! empty( $settings['color'] ) ? $settings['color'] : '#1a1a2e'; | |
| 159 | - $font_size = ! empty( $settings['font_size'] ) ? $settings['font_size'] : '32px'; | |
| 160 | - $title_text = ! empty( $settings['title'] ) ? $settings['title'] : $post->post_title; | |
| 161 | - $align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'left'; | |
| 172 | + $user_color = self::css_hex_color( $settings['color'] ?? '' ); | |
| 173 | + $user_size = self::css_font_size( $settings['font_size'] ?? '' ); | |
| 174 | + // isset(), not ! empty(): clearing the field is an instruction, not | |
| 175 | + // an accident. Only a title that was never set at all falls back to | |
| 176 | + // the campaign name — an emptied one stays empty (and the builder | |
| 177 | + // shows a placeholder in its place so it can still be selected). | |
| 178 | + $title_text = isset( $settings['title'] ) ? (string) $settings['title'] : $post->post_title; | |
| 179 | + $align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'left'; | |
| 180 | + | |
| 181 | + // A user-set value is emitted with !important so it wins over | |
| 182 | + // template rules (some templates force the title colour/size with | |
| 183 | + // !important). When unset we emit a plain default the template can | |
| 184 | + // still override. | |
| 185 | + $title_style = '' !== $user_color ? 'color:' . $user_color . ' !important;' : 'color:#1a1a2e;'; | |
| 186 | + $title_style .= '' !== $user_size ? 'font-size:' . $user_size . ' !important;' : 'font-size:32px;'; | |
| 187 | + $title_style .= self::decls_to_style( self::typography_common_decls( $settings ) ); | |
| 188 | + $title_style .= 'text-align:' . $align . ';'; | |
| 162 | 189 | ?> |
| 163 | - <h2 class="bp-campaign-title" | |
| 164 | - style="color: <?php echo esc_attr( $color ); ?>; font-size: <?php echo esc_attr( $font_size ); ?>; text-align: <?php echo esc_attr( $align ); ?>;"> | |
| 190 | + <h2 class="bp-campaign-title" style="<?php echo esc_attr( $title_style ); ?>"> | |
| 165 | 191 | <?php echo esc_html( $title_text ); ?> |
| 166 | 192 | </h2> |
| 167 | 193 | <?php |
| 168 | 194 | break; |
| @@ -167,26 +193,50 @@ | ||
| 167 | 193 | <?php |
| 168 | 194 | break; |
| 169 | 195 | |
| 170 | 196 | case 'campaign_description': |
| 171 | - $desc_headline = ! empty( $settings['headline'] ) ? $settings['headline'] : ''; | |
| 172 | - $desc_content = ! empty( $settings['content'] ) ? $settings['content'] : $post->post_content; | |
| 173 | - $desc_width = isset( $settings['width'] ) ? max( 10, min( 100, (int) $settings['width'] ) ) : 100; | |
| 197 | + // isset(), not ! empty(), for the same reason as the title above: | |
| 198 | + // clearing the body means "no body", not "show the post content". | |
| 199 | + $desc_headline = isset( $settings['headline'] ) ? (string) $settings['headline'] : ''; | |
| 200 | + $desc_content = isset( $settings['content'] ) ? (string) $settings['content'] : $post->post_content; | |
| 201 | + $desc_width = self::resolve_element_width( $settings ); | |
| 174 | 202 | $desc_align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'left'; |
| 175 | 203 | |
| 176 | 204 | if ( ! $desc_headline && ! $desc_content ) break; |
| 177 | 205 | |
| 206 | + // Wrapper carries layout only. Typography is applied directly to the | |
| 207 | + // headline and content elements (with !important) so it beats template | |
| 208 | + // rules that target those elements specifically. The headline (title) | |
| 209 | + // and the body each have their own independent typography set. | |
| 178 | 210 | $desc_wrap = 'width:' . $desc_width . '%;'; |
| 179 | 211 | if ( 'center' === $desc_align ) $desc_wrap .= 'margin:0 auto;'; |
| 180 | 212 | elseif ( 'right' === $desc_align ) $desc_wrap .= 'margin-left:auto;'; |
| 213 | + | |
| 214 | + // Title (headline) typography — its own set under the `title_` | |
| 215 | + // prefixed keys. font-size IS applied here so the title can be | |
| 216 | + // sized directly (empty leaves the template heading scale). | |
| 217 | + $title_typo = self::prefixed_typography( $settings, 'title_' ); | |
| 218 | + $title_color = self::css_hex_color( $title_typo['color'] ?? '' ); | |
| 219 | + $title_size = self::css_font_size( $title_typo['font_size'] ?? '' ); | |
| 220 | + $desc_headline_style = self::decls_to_style( self::typography_common_decls( $title_typo ) ); | |
| 221 | + if ( '' !== $title_color ) $desc_headline_style .= 'color:' . $title_color . ' !important;'; | |
| 222 | + if ( '' !== $title_size ) $desc_headline_style .= 'font-size:' . $title_size . ' !important;'; | |
| 223 | + | |
| 224 | + // Body (content) typography — the base (unprefixed) keys, so any | |
| 225 | + // previously-saved description typography still applies here. | |
| 226 | + $desc_color = self::css_hex_color( $settings['color'] ?? '' ); | |
| 227 | + $desc_size = self::css_font_size( $settings['font_size'] ?? '' ); | |
| 228 | + $desc_content_style = self::decls_to_style( self::typography_common_decls( $settings ) ); | |
| 229 | + if ( '' !== $desc_color ) $desc_content_style .= 'color:' . $desc_color . ' !important;'; | |
| 230 | + if ( '' !== $desc_size ) $desc_content_style .= 'font-size:' . $desc_size . ' !important;'; | |
| 181 | 231 | ?> |
| 182 | 232 | <div class="bp-campaign-description" |
| 183 | 233 | style="text-align:<?php echo esc_attr( $desc_align ); ?>; <?php echo esc_attr( $desc_wrap ); ?>"> |
| 184 | 234 | <?php if ( $desc_headline ) : ?> |
| 185 | - <h3 class="bp-campaign-description-headline"><?php echo esc_html( $desc_headline ); ?></h3> | |
| 235 | + <h3 class="bp-campaign-description-headline"<?php echo '' !== $desc_headline_style ? ' style="' . esc_attr( $desc_headline_style ) . '"' : ''; ?>><?php echo esc_html( $desc_headline ); ?></h3> | |
| 186 | 236 | <?php endif; ?> |
| 187 | 237 | <?php if ( $desc_content ) : ?> |
| 188 | - <div class="bp-campaign-description-content"><?php echo wp_kses_post( self::scale_inline_font_sizes( $desc_content ) ); ?></div> | |
| 238 | + <div class="bp-campaign-description-content"<?php echo '' !== $desc_content_style ? ' style="' . esc_attr( $desc_content_style ) . '"' : ''; ?>><?php echo wp_kses_post( self::scale_inline_font_sizes( $desc_content ) ); ?></div> | |
| 189 | 239 | <?php endif; ?> |
| 190 | 240 | </div> |
| 191 | 241 | <?php |
| 192 | 242 | break; |
| @@ -212,9 +262,9 @@ | ||
| 212 | 262 | if ( ! $src ) { |
| 213 | 263 | $src = ! empty( $settings['src'] ) ? $settings['src'] : get_the_post_thumbnail_url( $campaign_id, $size ); |
| 214 | 264 | } |
| 215 | 265 | $alt_text = ! empty( $settings['alt'] ) ? $settings['alt'] : $post->post_title; |
| 216 | - $ph_width = isset( $settings['width'] ) ? max( 10, min( 100, (int) $settings['width'] ) ) : 100; | |
| 266 | + $ph_width = self::resolve_element_width( $settings ); | |
| 217 | 267 | $ph_align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'center'; |
| 218 | 268 | |
| 219 | 269 | if ( $src ) : |
| 220 | 270 | $img_style = 'width:auto;max-width:' . $ph_width . '%;height:auto;display:block;border-radius:4px;'; |
| @@ -248,14 +298,23 @@ | ||
| 248 | 298 | $show_goal = isset( $settings['show_goal'] ) ? (bool) $settings['show_goal'] : true; |
| 249 | 299 | $round_amounts = (bool) ( $settings['round_amounts'] ?? false ); |
| 250 | 300 | $donate_label = ! empty( $settings['donate_label'] ) ? $settings['donate_label'] : __( 'Donated:', 'better-payment' ); |
| 251 | 301 | $goal_label = ! empty( $settings['goal_label'] ) ? $settings['goal_label'] : __( 'Goal:', 'better-payment' ); |
| 252 | - $width = isset( $settings['width'] ) ? absint( $settings['width'] ) : 100; | |
| 253 | - $align = ! empty( $settings['align'] ) ? $settings['align'] : 'left'; | |
| 254 | - $align_map = [ 'left' => 'flex-start', 'center' => 'center', 'right' => 'flex-end' ]; | |
| 255 | - $justify = $align_map[ $align ] ?? 'flex-start'; | |
| 302 | + $width = self::resolve_element_width( $settings ); | |
| 303 | + $align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'left'; | |
| 256 | 304 | $currency_sym = self::currency_symbol( $currency ); |
| 257 | 305 | |
| 306 | + // Align the block itself within its column via auto margins — the | |
| 307 | + // same width/margin pattern every other element uses. (A bare | |
| 308 | + // `justify-content` did nothing here: `.bp-campaign-progress` is | |
| 309 | + // not a flex container, so center/right never took effect.) | |
| 310 | + $wrap_style = 'width:' . $width . '%;'; | |
| 311 | + if ( 'center' === $align ) { | |
| 312 | + $wrap_style .= 'margin:0 auto;'; | |
| 313 | + } elseif ( 'right' === $align ) { | |
| 314 | + $wrap_style .= 'margin-left:auto;'; | |
| 315 | + } | |
| 316 | + | |
| 258 | 317 | // Progress label: ceiling (rounded up integer) when round_amounts, else 1 decimal place. |
| 259 | 318 | if ( $round_amounts ) { |
| 260 | 319 | $display_progress = (int) ceil( $goal > 0 ? min( 100, ( $raised / $goal ) * 100 ) : 0 ); |
| 261 | 320 | $goal_fmt = number_format( (int) ceil( $goal ) ); |
| @@ -260,17 +319,14 @@ | ||
| 260 | 319 | $display_progress = (int) ceil( $goal > 0 ? min( 100, ( $raised / $goal ) * 100 ) : 0 ); |
| 261 | 320 | $goal_fmt = number_format( (int) ceil( $goal ) ); |
| 262 | 321 | } else { |
| 263 | 322 | $display_progress = $progress; // 1 decimal float from CampaignStats |
| 264 | - $goal_fmt = number_format( $goal, 2 ); | |
| 323 | + $goal_fmt = Money::format( $goal ); | |
| 265 | 324 | } |
| 266 | 325 | |
| 267 | - if ( $goal <= 0 ) { | |
| 268 | - break; | |
| 269 | - } | |
| 270 | 326 | ?> |
| 271 | 327 | <div class="bp-campaign-progress" |
| 272 | - style="width:<?php echo esc_attr( $width ); ?>%; justify-content:<?php echo esc_attr( $justify ); ?>"> | |
| 328 | + style="<?php echo esc_attr( $wrap_style ); ?>"> | |
| 273 | 329 | <?php if ( $headline ) : ?> |
| 274 | 330 | <h3 class="bp-progress-headline"><?php echo esc_html( $headline ); ?></h3> |
| 275 | 331 | <?php endif; ?> |
| 276 | 332 | <div class="bp-progress-bar-wrap"> |
| @@ -301,9 +357,9 @@ | ||
| 301 | 357 | $show_raised = (bool) ( $settings['show_raised'] ?? true ); |
| 302 | 358 | $show_donors = (bool) ( $settings['show_donors'] ?? true ); |
| 303 | 359 | $show_percent = (bool) ( $settings['show_percent'] ?? true ); |
| 304 | 360 | $show_days = (bool) ( $settings['show_days'] ?? true ); |
| 305 | - $sm_width = isset( $settings['width'] ) ? max( 10, min( 100, (int) $settings['width'] ) ) : 100; | |
| 361 | + $sm_width = self::resolve_element_width( $settings ); | |
| 306 | 362 | $sm_align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'left'; |
| 307 | 363 | $goal = (float) ( $meta['bpc_goal_amount'] ?? 0 ); |
| 308 | 364 | $percent = $goal > 0 ? min( 100, round( ( (float) $stats['total_raised'] / $goal ) * 100, 1 ) ) : 0; |
| 309 | 365 | $sm_currency = self::global_currency(); |
| @@ -319,9 +375,9 @@ | ||
| 319 | 375 | <?php endif; ?> |
| 320 | 376 | <div class="bp-campaign-summary"> |
| 321 | 377 | <?php if ( $show_raised ) : ?> |
| 322 | 378 | <div class="bp-summary-item"> |
| 323 | - <strong><?php echo esc_html( $sm_currency_sym . number_format( (float) $stats['total_raised'], 2 ) ); ?></strong> | |
| 379 | + <strong><?php echo esc_html( Money::with_symbol( $sm_currency_sym, $stats['total_raised'] ) ); ?></strong> | |
| 324 | 380 | <span><?php esc_html_e( 'Raised', 'better-payment' ); ?></span> |
| 325 | 381 | </div> |
| 326 | 382 | <?php endif; ?> |
| 327 | 383 | <?php if ( $show_donors ) : ?> |
| @@ -329,17 +385,17 @@ | ||
| 329 | 385 | <strong><?php echo esc_html( $stats['donor_count'] ); ?></strong> |
| 330 | 386 | <span><?php esc_html_e( 'Donors', 'better-payment' ); ?></span> |
| 331 | 387 | </div> |
| 332 | 388 | <?php endif; ?> |
| 333 | - <?php if ( $show_percent && $goal > 0 ) : ?> | |
| 389 | + <?php if ( $show_percent ) : ?> | |
| 334 | 390 | <div class="bp-summary-item"> |
| 335 | 391 | <strong><?php echo esc_html( $percent ); ?>%</strong> |
| 336 | 392 | <span><?php esc_html_e( 'Raised', 'better-payment' ); ?></span> |
| 337 | 393 | </div> |
| 338 | 394 | <?php endif; ?> |
| 339 | - <?php if ( $show_days && ! is_null( $stats['days_remaining'] ) ) : ?> | |
| 395 | + <?php if ( $show_days ) : ?> | |
| 340 | 396 | <div class="bp-summary-item"> |
| 341 | - <strong><?php echo esc_html( $stats['days_remaining'] ); ?></strong> | |
| 397 | + <strong><?php echo esc_html( is_null( $stats['days_remaining'] ) ? 0 : $stats['days_remaining'] ); ?></strong> | |
| 342 | 398 | <span><?php esc_html_e( 'Days Left', 'better-payment' ); ?></span> |
| 343 | 399 | </div> |
| 344 | 400 | <?php endif; ?> |
| 345 | 401 | </div> |
| @@ -347,19 +403,28 @@ | ||
| 347 | 403 | <?php |
| 348 | 404 | break; |
| 349 | 405 | |
| 350 | 406 | case 'donation_form': |
| 351 | - $button_label = ! empty( $settings['button_label'] ) | |
| 352 | - ? $settings['button_label'] | |
| 353 | - : ( ! empty( $settings['button_text'] ) ? $settings['button_text'] : __( 'Donate Now', 'better-payment' ) ); | |
| 407 | + // isset(), not ! empty(): an emptied label means "no label", and | |
| 408 | + // must not silently come back as "Donate Now". Only a label that was | |
| 409 | + // never set falls back — first to the legacy `button_text` key, then | |
| 410 | + // to the default. (`button_text` is the pre-rename key; keep reading | |
| 411 | + // it so campaigns built before the rename still show their label.) | |
| 412 | + if ( isset( $settings['button_label'] ) ) { | |
| 413 | + $button_label = (string) $settings['button_label']; | |
| 414 | + } elseif ( isset( $settings['button_text'] ) ) { | |
| 415 | + $button_label = (string) $settings['button_text']; | |
| 416 | + } else { | |
| 417 | + $button_label = __( 'Donate Now', 'better-payment' ); | |
| 418 | + } | |
| 354 | 419 | $primary = $meta['bpc_color_primary'] ?: '#6b63f6'; |
| 355 | 420 | $button_color = sanitize_hex_color( $settings['button_color'] ?? '' ) ?: sanitize_hex_color( $primary ) ?: '#6b63f6'; |
| 356 | 421 | $open_new_tab = ! empty( $settings['open_new_tab'] ); |
| 357 | - $width = isset( $settings['width'] ) ? max( 10, min( 100, (int) $settings['width'] ) ) : 100; | |
| 422 | + $width = self::resolve_element_width( $settings ); | |
| 358 | 423 | $align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) |
| 359 | 424 | ? $settings['align'] : 'center'; |
| 360 | 425 | |
| 361 | - // URL: element setting takes priority; fall back to campaign meta page ID, then '#'. | |
| 426 | + // URL: element setting takes priority; fall back to campaign meta page ID, then '#' default. | |
| 362 | 427 | $donate_url = ''; |
| 363 | 428 | if ( ! empty( $settings['url'] ) ) { |
| 364 | 429 | $donate_url = esc_url( $settings['url'] ); |
| 365 | 430 | } else { |
| @@ -367,12 +432,12 @@ | ||
| 367 | 432 | if ( $page_id ) { |
| 368 | 433 | $donate_url = esc_url( add_query_arg( 'campaign_id', $campaign_id, get_permalink( $page_id ) ) ); |
| 369 | 434 | } |
| 370 | 435 | } |
| 436 | + // Default the Payment Form Page URL to '#' so the button renders as a normal link. | |
| 371 | 437 | $url_missing = false; |
| 372 | 438 | if ( ! $donate_url ) { |
| 373 | - $donate_url = '#'; | |
| 374 | - $url_missing = true; | |
| 439 | + $donate_url = '#'; | |
| 375 | 440 | } |
| 376 | 441 | |
| 377 | 442 | if ( $donate_url ) : |
| 378 | 443 | $min_amount = isset( $meta['bpc_minimum_amount'] ) && $meta['bpc_minimum_amount'] !== '' |
| @@ -396,9 +461,9 @@ | ||
| 396 | 461 | <?php |
| 397 | 462 | printf( |
| 398 | 463 | /* translators: %s: formatted minimum amount with currency symbol */ |
| 399 | 464 | esc_html__( 'The minimum donation for this campaign is %s.', 'better-payment' ), |
| 400 | - esc_html( $currency_symbol . number_format( $min_amount, 2 ) ) | |
| 465 | + esc_html( Money::with_symbol( $currency_symbol, $min_amount ) ) | |
| 401 | 466 | ); |
| 402 | 467 | ?> |
| 403 | 468 | </p> |
| 404 | 469 | <?php endif; ?> |
| @@ -404,9 +469,9 @@ | ||
| 404 | 469 | <?php endif; ?> |
| 405 | 470 | <div class="bp-campaign-donate-btn" style="<?php echo esc_attr( $wrap_style ); ?>"> |
| 406 | 471 | <a href="<?php echo $url_missing ? '#' : esc_url( $donate_url ); ?>" |
| 407 | 472 | class="<?php echo esc_attr( $btn_class ); ?>" |
| 408 | - style="background-color:<?php echo esc_attr( $button_color ); ?>;" | |
| 473 | + style="background-color:<?php echo esc_attr( $button_color ); ?> !important;" | |
| 409 | 474 | <?php if ( $url_missing ) : ?> |
| 410 | 475 | aria-disabled="true" |
| 411 | 476 | title="<?php esc_attr_e( 'Payment page not configured', 'better-payment' ); ?>" |
| 412 | 477 | <?php else : ?> |
| @@ -426,9 +491,9 @@ | ||
| 426 | 491 | ? (int) $settings['creator_user_id'] |
| 427 | 492 | : (int) $post->post_author; |
| 428 | 493 | $role_title = ! empty( $settings['role_title'] ) ? $settings['role_title'] : __( 'Organizer', 'better-payment' ); |
| 429 | 494 | $description = ! empty( $settings['description'] ) ? $settings['description'] : ''; |
| 430 | - $width = isset( $settings['width'] ) ? max( 10, min( 100, (int) $settings['width'] ) ) : 100; | |
| 495 | + $width = self::resolve_element_width( $settings ); | |
| 431 | 496 | $align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) |
| 432 | 497 | ? $settings['align'] : 'left'; |
| 433 | 498 | |
| 434 | 499 | $creator = get_user_by( 'ID', $creator_user_id ); |
| @@ -460,9 +525,21 @@ | ||
| 460 | 525 | $amounts_meta = $meta['bpc_suggested_amounts'] ?? []; |
| 461 | 526 | $allow_custom = (bool) ( $meta['bpc_allow_custom_amount'] ?? 1 ); |
| 462 | 527 | $currency = self::global_currency(); |
| 463 | 528 | $currency_symbol = self::currency_symbol( $currency ); |
| 529 | + $da_headline = isset( $settings['headline'] ) ? $settings['headline'] : __( 'Donate Amount', 'better-payment' ); | |
| 530 | + $da_width = self::resolve_element_width( $settings ); | |
| 531 | + $da_align = in_array( $settings['align'] ?? '', [ 'left', 'center', 'right' ], true ) ? $settings['align'] : 'left'; | |
| 464 | 532 | |
| 533 | + // Align the block within its column via auto margins — same | |
| 534 | + // width/margin pattern as every other element. | |
| 535 | + $da_wrap_style = 'width:' . $da_width . '%;'; | |
| 536 | + if ( 'center' === $da_align ) { | |
| 537 | + $da_wrap_style .= 'margin:0 auto;'; | |
| 538 | + } elseif ( 'right' === $da_align ) { | |
| 539 | + $da_wrap_style .= 'margin-left:auto;'; | |
| 540 | + } | |
| 541 | + | |
| 465 | 542 | // Fall back to legacy comma-string or defaults when meta is empty. |
| 466 | 543 | if ( empty( $amounts_meta ) ) { |
| 467 | 544 | $fallback = ! empty( $settings['preset_amounts'] ) ? $settings['preset_amounts'] : '10,25,50,100'; |
| 468 | 545 | foreach ( array_filter( array_map( 'trim', explode( ',', $fallback ) ) ) as $a ) { |
| @@ -469,9 +546,12 @@ | ||
| 469 | 546 | $amounts_meta[] = [ 'amount' => $a, 'is_default' => false ]; |
| 470 | 547 | } |
| 471 | 548 | } |
| 472 | 549 | ?> |
| 473 | - <div class="bp-campaign-donate"> | |
| 550 | + <div class="bp-campaign-donate" style="<?php echo esc_attr( $da_wrap_style ); ?>"> | |
| 551 | + <?php if ( $da_headline ) : ?> | |
| 552 | + <h3 class="bp-donate-headline"><?php echo esc_html( $da_headline ); ?></h3> | |
| 553 | + <?php endif; ?> | |
| 474 | 554 | <div class="bp-donate_amounts"> |
| 475 | 555 | <?php foreach ( $amounts_meta as $i => $item ) : |
| 476 | 556 | $amt = floatval( $item['amount'] ?? 0 ); |
| 477 | 557 | $uid = 'bp_camt_' . $campaign_id . '_' . $i; |
| @@ -486,9 +566,9 @@ | ||
| 486 | 566 | <?php checked( $is_default ); ?> |
| 487 | 567 | hidden |
| 488 | 568 | /> |
| 489 | 569 | <label for="<?php echo esc_attr( $uid ); ?>" class="bp-amount-label"> |
| 490 | - <?php echo esc_html( $currency_symbol . $amt ); ?> | |
| 570 | + <?php echo esc_html( Money::with_symbol( $currency_symbol, $amt ) ); ?> | |
| 491 | 571 | </label> |
| 492 | 572 | <?php endforeach; ?> |
| 493 | 573 | </div> |
| 494 | 574 | <?php if ( $allow_custom ) : ?> |
| @@ -529,26 +609,27 @@ | ||
| 529 | 609 | $active_sharing = array_filter( $share_links, function( $url, $key ) use ( $settings ) { |
| 530 | 610 | return ( $settings[ $key ] ?? true ) !== false; |
| 531 | 611 | }, ARRAY_FILTER_USE_BOTH ); |
| 532 | 612 | |
| 533 | - if ( $active_sharing || $sh_headline ) : | |
| 613 | + // Same as social_links above: with every network switched off there | |
| 614 | + // is nothing to share, so a lone "Share Now" heading would sit on the | |
| 615 | + // page labelling nothing. | |
| 616 | + if ( $active_sharing ) : | |
| 534 | 617 | ?> |
| 535 | 618 | <div class="bp-social-sharing" style="text-align:<?php echo esc_attr( $sh_align ); ?>;"> |
| 536 | 619 | <?php if ( $sh_headline ) : ?> |
| 537 | 620 | <p class="bp-social-headline"><?php echo esc_html( $sh_headline ); ?></p> |
| 538 | 621 | <?php endif; ?> |
| 539 | - <?php if ( $active_sharing ) : ?> | |
| 540 | - <div class="bp-social-icons" style="justify-content:<?php echo esc_attr( $sh_justify ); ?>;"> | |
| 541 | - <?php foreach ( $active_sharing as $key => $share_url ) : ?> | |
| 542 | - <a href="<?php echo esc_url( $share_url ); ?>" | |
| 543 | - class="bp-social-icon" | |
| 544 | - <?php echo $sh_target; ?> | |
| 545 | - title="<?php echo esc_attr( ucfirst( $key ) ); ?>"> | |
| 546 | - <?php echo self::social_icon_svg( $key ); ?> | |
| 547 | - </a> | |
| 548 | - <?php endforeach; ?> | |
| 549 | - </div> | |
| 550 | - <?php endif; ?> | |
| 622 | + <div class="bp-social-icons" style="justify-content:<?php echo esc_attr( $sh_justify ); ?>;"> | |
| 623 | + <?php foreach ( $active_sharing as $key => $share_url ) : ?> | |
| 624 | + <a href="<?php echo esc_url( $share_url ); ?>" | |
| 625 | + class="bp-social-icon" | |
| 626 | + <?php echo $sh_target; ?> | |
| 627 | + title="<?php echo esc_attr( ucfirst( $key ) ); ?>"> | |
| 628 | + <?php echo self::social_icon_svg( $key ); ?> | |
| 629 | + </a> | |
| 630 | + <?php endforeach; ?> | |
| 631 | + </div> | |
| 551 | 632 | </div> |
| 552 | 633 | <?php |
| 553 | 634 | endif; |
| 554 | 635 | break; |
| @@ -567,34 +648,80 @@ | ||
| 567 | 648 | $active_links[ $key ] = $settings[ $key ]; |
| 568 | 649 | } |
| 569 | 650 | } |
| 570 | 651 | |
| 571 | - if ( $active_links || $sl_headline ) : | |
| 652 | + // The links are the widget; the headline only labels them. With no | |
| 653 | + // links there is nothing to label, so a lone "Follow Now" heading is | |
| 654 | + // a promise the page cannot keep — render nothing instead. (Was | |
| 655 | + // `$active_links || $sl_headline`, which kept the heading alive on | |
| 656 | + // its own because the headline defaults to a non-empty string.) | |
| 657 | + if ( $active_links ) : | |
| 572 | 658 | ?> |
| 573 | 659 | <div class="bp-social-links" style="text-align:<?php echo esc_attr( $sl_align ); ?>;"> |
| 574 | 660 | <?php if ( $sl_headline ) : ?> |
| 575 | 661 | <p class="bp-social-headline"><?php echo esc_html( $sl_headline ); ?></p> |
| 576 | 662 | <?php endif; ?> |
| 577 | - <?php if ( $active_links ) : ?> | |
| 578 | - <div class="bp-social-icons" style="justify-content:<?php echo esc_attr( $sl_justify ); ?>;"> | |
| 579 | - <?php foreach ( $active_links as $key => $url ) : ?> | |
| 580 | - <a href="<?php echo esc_url( $url ); ?>" | |
| 581 | - class="bp-social-icon" | |
| 582 | - <?php echo $sl_target; ?> | |
| 583 | - title="<?php echo esc_attr( ucfirst( $key ) ); ?>"> | |
| 584 | - <?php echo self::social_icon_svg( $key ); ?> | |
| 585 | - </a> | |
| 586 | - <?php endforeach; ?> | |
| 587 | - </div> | |
| 588 | - <?php endif; ?> | |
| 663 | + <div class="bp-social-icons" style="justify-content:<?php echo esc_attr( $sl_justify ); ?>;"> | |
| 664 | + <?php foreach ( $active_links as $key => $url ) : ?> | |
| 665 | + <a href="<?php echo esc_url( $url ); ?>" | |
| 666 | + class="bp-social-icon" | |
| 667 | + <?php echo $sl_target; ?> | |
| 668 | + title="<?php echo esc_attr( ucfirst( $key ) ); ?>"> | |
| 669 | + <?php echo self::social_icon_svg( $key ); ?> | |
| 670 | + </a> | |
| 671 | + <?php endforeach; ?> | |
| 672 | + </div> | |
| 589 | 673 | </div> |
| 590 | 674 | <?php |
| 591 | 675 | endif; |
| 592 | 676 | break; |
| 677 | + | |
| 678 | + default: | |
| 679 | + // Extensibility seam: element types registered by add-ons (e.g. the | |
| 680 | + // Pro plugin, via the `better_payment/campaign_elements` filter) are | |
| 681 | + // not handled by any case above. Dispatch a type-specific filter so | |
| 682 | + // the add-on can render its own markup. The listener receives the | |
| 683 | + // full render context and MUST return already-escaped HTML (same | |
| 684 | + // contract as the built-in cases, which echo markup directly). | |
| 685 | + // | |
| 686 | + // With no listener (e.g. Pro deactivated) the filter returns '', | |
| 687 | + // so a saved layout containing an unknown type degrades to empty | |
| 688 | + // output instead of fataling. | |
| 689 | + $ctx = [ | |
| 690 | + 'element' => $element, | |
| 691 | + 'settings' => $settings, | |
| 692 | + 'campaign_id' => $campaign_id, | |
| 693 | + 'post' => $post, | |
| 694 | + 'meta' => $meta, | |
| 695 | + 'stats' => $stats, | |
| 696 | + 'is_preview' => $is_preview, | |
| 697 | + ]; | |
| 698 | + echo apply_filters( "better_payment/campaign/render_element_{$type}", '', $ctx ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 699 | + break; | |
| 593 | 700 | } |
| 594 | 701 | |
| 595 | 702 | $html = ob_get_clean(); |
| 596 | 703 | |
| 704 | + // An element with nothing to show needs opposite treatment in the two | |
| 705 | + // places it renders, and this is the one point every element type passes | |
| 706 | + // through — free, Pro, and anything added later — so neither behaviour can | |
| 707 | + // be reintroduced by an individual widget. | |
| 708 | + // | |
| 709 | + // Builder: substitute a placeholder. Without it the element occupies no | |
| 710 | + // space, so there is nothing to see, click, drag or delete — and because | |
| 711 | + // the `data-bp-element-id` wrapper below is skipped for empty output, the | |
| 712 | + // canvas cannot even place a hotspot over it. The author is left with an | |
| 713 | + // invisible element they cannot reach. | |
| 714 | + // | |
| 715 | + // Frontend: render nothing at all. "Renders nothing" is not the same as | |
| 716 | + // "is an empty string": an emptied Title still emits an <h2> that occupies | |
| 717 | + // a 32px line, and an emptied Donate button a coloured bar with no label. | |
| 718 | + // Both are holes in a live campaign page, so they are dropped entirely | |
| 719 | + // rather than shipped as blank boxes. | |
| 720 | + if ( self::renders_nothing_visible( (string) $html ) ) { | |
| 721 | + $html = $is_preview ? self::empty_element_placeholder( $type ) : ''; | |
| 722 | + } | |
| 723 | + | |
| 597 | 724 | if ( ! $html || ! $el_id ) { |
| 598 | 725 | return $html; |
| 599 | 726 | } |
| 600 | 727 | |
| @@ -601,8 +728,70 @@ | ||
| 601 | 728 | return '<div class="bp-element-wrap" data-bp-element-id="' . esc_attr( $el_id ) . '">' . $html . '</div>'; |
| 602 | 729 | } |
| 603 | 730 | |
| 604 | 731 | /** |
| 732 | + * Does this element's markup put nothing on the page a reader could see? | |
| 733 | + * | |
| 734 | + * Not the same as an empty string. An emptied Title still renders | |
| 735 | + * `<h1 class="bp-campaign-title" style="…"></h1>`, and a Social Links block | |
| 736 | + * with no URLs still renders its wrapper — both are markup, and both are | |
| 737 | + * invisible. Only checking for `''` would leave those elements unreachable on | |
| 738 | + * the canvas, which is the bug this whole placeholder exists to prevent. | |
| 739 | + * | |
| 740 | + * Errs firmly toward "not empty": wrongly blanking an element that HAS | |
| 741 | + * content would hide the author's work, which is far worse than missing a | |
| 742 | + * placeholder. So anything that can paint pixels without text — an image, | |
| 743 | + * an embed, a form control, a chart — counts as content immediately, and only | |
| 744 | + * markup with no such node AND no visible text is called empty. | |
| 745 | + * | |
| 746 | + * @param string $html Rendered element markup. | |
| 747 | + * @return bool True when the element would show nothing. | |
| 748 | + */ | |
| 749 | + private static function renders_nothing_visible( string $html ): bool { | |
| 750 | + if ( '' === trim( $html ) ) { | |
| 751 | + return true; | |
| 752 | + } | |
| 753 | + | |
| 754 | + // Nodes that show something without needing text content. If any is | |
| 755 | + // present the element is showing the author something real. | |
| 756 | + $visual = '#<(img|svg|iframe|video|audio|canvas|picture|object|embed|input|select|textarea|progress|meter|hr|table)[\s/>]#i'; | |
| 757 | + if ( preg_match( $visual, $html ) ) { | |
| 758 | + return false; | |
| 759 | + } | |
| 760 | + | |
| 761 | + // Otherwise it is only worth showing if it renders visible text. | |
| 762 | + // wp_strip_all_tags() also drops <script>/<style> bodies, so an element | |
| 763 | + // whose only output is a style block reads as empty — which it is. | |
| 764 | + return '' === trim( wp_strip_all_tags( $html ) ); | |
| 765 | + } | |
| 766 | + | |
| 767 | + /** | |
| 768 | + * Builder-only stand-in for an element that currently renders nothing. | |
| 769 | + * | |
| 770 | + * Deliberately empty: an outline and a minimum height, no icon, label or | |
| 771 | + * copy. The element's identity and its settings already live in the panel on | |
| 772 | + * the right the moment it is selected, so text in the canvas would repeat | |
| 773 | + * what the UI says anyway — and a widget's job is to show the author's | |
| 774 | + * content, not ours. All this needs to do is give the element enough of a | |
| 775 | + * body to be seen, clicked, dragged and deleted. | |
| 776 | + * | |
| 777 | + * Styles are inline rather than classed: this renders in two different | |
| 778 | + * preview documents (the builder canvas and the template picker) and only one | |
| 779 | + * of them ships a <style> block, so a self-contained placeholder cannot be | |
| 780 | + * broken by rendering in the wrong one. | |
| 781 | + * | |
| 782 | + * @param string $type Element type slug, exposed as a data attribute for | |
| 783 | + * debugging and E2E selectors — never shown to the user. | |
| 784 | + * @return string Placeholder HTML. | |
| 785 | + */ | |
| 786 | + private static function empty_element_placeholder( string $type ): string { | |
| 787 | + $box = 'min-height:64px;border:1px dashed #c7cede;border-radius:8px;background:#fbfcfe;'; | |
| 788 | + | |
| 789 | + return '<div class="bp-element-empty" style="' . esc_attr( $box ) . '"' | |
| 790 | + . ' data-bp-empty-type="' . esc_attr( $type ) . '"></div>'; | |
| 791 | + } | |
| 792 | + | |
| 793 | + /** | |
| 605 | 794 | * Normalize raw stored layout to the column schema. |
| 606 | 795 | * Handles both old flat-array format and new column format. |
| 607 | 796 | * |
| 608 | 797 | * @param mixed $raw Value from MetaBox::get_all()['bpc_fields_layout']. |
| @@ -667,8 +856,249 @@ | ||
| 667 | 856 | ); |
| 668 | 857 | } |
| 669 | 858 | |
| 670 | 859 | /** |
| 860 | + * Normalize a stored font-size setting into a safe CSS length. | |
| 861 | + * Numeric values are treated as pixels. Returns '' when unset/invalid. | |
| 862 | + * | |
| 863 | + * @param mixed $val | |
| 864 | + */ | |
| 865 | + private static function css_font_size( $val ): string { | |
| 866 | + if ( is_string( $val ) ) { | |
| 867 | + $val = trim( $val ); | |
| 868 | + } | |
| 869 | + if ( '' === $val || null === $val ) { | |
| 870 | + return ''; | |
| 871 | + } | |
| 872 | + if ( is_numeric( $val ) ) { | |
| 873 | + return ( (float) $val ) . 'px'; | |
| 874 | + } | |
| 875 | + if ( is_string( $val ) && preg_match( '/^\d+(\.\d+)?(px|em|rem|%)$/', $val ) ) { | |
| 876 | + return $val; | |
| 877 | + } | |
| 878 | + return ''; | |
| 879 | + } | |
| 880 | + | |
| 881 | + /** | |
| 882 | + * Validate a stored font-family stack. Allows letters, numbers, spaces, | |
| 883 | + * commas, quotes and hyphens only — blocks CSS breakout. Returns '' when | |
| 884 | + * unset/invalid. | |
| 885 | + * | |
| 886 | + * @param mixed $val | |
| 887 | + */ | |
| 888 | + private static function css_font_family( $val ): string { | |
| 889 | + $val = is_string( $val ) ? trim( $val ) : ''; | |
| 890 | + if ( '' === $val ) { | |
| 891 | + return ''; | |
| 892 | + } | |
| 893 | + return preg_match( "/^[A-Za-z0-9 ,'\"_-]+$/", $val ) ? $val : ''; | |
| 894 | + } | |
| 895 | + | |
| 896 | + /** | |
| 897 | + * Validate a stored font-style value. Returns '' when unset/invalid. | |
| 898 | + * | |
| 899 | + * @param mixed $val | |
| 900 | + */ | |
| 901 | + private static function css_font_style( $val ): string { | |
| 902 | + $val = is_string( $val ) ? strtolower( trim( $val ) ) : ''; | |
| 903 | + return in_array( $val, [ 'normal', 'italic', 'oblique' ], true ) ? $val : ''; | |
| 904 | + } | |
| 905 | + | |
| 906 | + /** | |
| 907 | + * Validate a stored hex color. Returns '' when unset/invalid. | |
| 908 | + * | |
| 909 | + * @param mixed $val | |
| 910 | + */ | |
| 911 | + private static function css_hex_color( $val ): string { | |
| 912 | + $val = is_string( $val ) ? trim( $val ) : ''; | |
| 913 | + return preg_match( '/^#[0-9a-fA-F]{3,8}$/', $val ) ? $val : ''; | |
| 914 | + } | |
| 915 | + | |
| 916 | + /** | |
| 917 | + * Normalize a stored CSS length (letter/word spacing, line-height). Numeric | |
| 918 | + * values are treated as pixels; a leading minus is allowed. Returns '' when | |
| 919 | + * unset/invalid. | |
| 920 | + * | |
| 921 | + * @param mixed $val | |
| 922 | + */ | |
| 923 | + /** | |
| 924 | + * Resolve an element's percentage width setting. | |
| 925 | + * | |
| 926 | + * A blank or non-positive width means "use the default" — never 0. Without | |
| 927 | + * this guard an empty-string width (e.g. produced by the AI layer) becomes | |
| 928 | + * `(int) '' = 0`, collapsing the element to a sliver. Valid values are | |
| 929 | + * clamped to 10–100%. | |
| 930 | + * | |
| 931 | + * @param array $settings | |
| 932 | + * @param int $default | |
| 933 | + */ | |
| 934 | + private static function resolve_element_width( array $settings, int $default = 100 ): int { | |
| 935 | + if ( ! isset( $settings['width'] ) || '' === $settings['width'] ) { | |
| 936 | + return $default; | |
| 937 | + } | |
| 938 | + $width = (int) $settings['width']; | |
| 939 | + if ( $width <= 0 ) { | |
| 940 | + return $default; | |
| 941 | + } | |
| 942 | + return max( 10, min( 100, $width ) ); | |
| 943 | + } | |
| 944 | + | |
| 945 | + /** | |
| 946 | + * Render a line-height value. Unlike other lengths, CSS `line-height` is | |
| 947 | + * unitless-capable: a small value (< 4) is a multiplier (e.g. 1.5) and must | |
| 948 | + * NOT be emitted as pixels — `line-height: 1.5px` collapses every line on top | |
| 949 | + * of the next. Larger values are treated as pixels (the builder's control is | |
| 950 | + * labelled "px"). | |
| 951 | + * | |
| 952 | + * @param mixed $val | |
| 953 | + */ | |
| 954 | + private static function css_line_height( $val ): string { | |
| 955 | + if ( is_string( $val ) ) { | |
| 956 | + $val = trim( $val ); | |
| 957 | + } | |
| 958 | + if ( '' === $val || null === $val ) { | |
| 959 | + return ''; | |
| 960 | + } | |
| 961 | + if ( is_numeric( $val ) ) { | |
| 962 | + $num = (float) $val; | |
| 963 | + if ( $num > 0 && $num < 4 ) { | |
| 964 | + return (string) $num; // unitless multiplier | |
| 965 | + } | |
| 966 | + return $num . 'px'; | |
| 967 | + } | |
| 968 | + if ( is_string( $val ) && preg_match( '/^-?\d+(\.\d+)?(px|em|rem|%)$/', $val ) ) { | |
| 969 | + return $val; | |
| 970 | + } | |
| 971 | + return ''; | |
| 972 | + } | |
| 973 | + | |
| 974 | + private static function css_length( $val ): string { | |
| 975 | + if ( is_string( $val ) ) { | |
| 976 | + $val = trim( $val ); | |
| 977 | + } | |
| 978 | + if ( '' === $val || null === $val ) { | |
| 979 | + return ''; | |
| 980 | + } | |
| 981 | + if ( is_numeric( $val ) ) { | |
| 982 | + return ( (float) $val ) . 'px'; | |
| 983 | + } | |
| 984 | + if ( is_string( $val ) && preg_match( '/^-?\d+(\.\d+)?(px|em|rem|%)$/', $val ) ) { | |
| 985 | + return $val; | |
| 986 | + } | |
| 987 | + return ''; | |
| 988 | + } | |
| 989 | + | |
| 990 | + /** | |
| 991 | + * Validate a stored font-weight value. Returns '' when unset/invalid. | |
| 992 | + * | |
| 993 | + * @param mixed $val | |
| 994 | + */ | |
| 995 | + private static function css_font_weight( $val ): string { | |
| 996 | + if ( is_int( $val ) ) { | |
| 997 | + $val = (string) $val; | |
| 998 | + } | |
| 999 | + $val = is_string( $val ) ? strtolower( trim( $val ) ) : ''; | |
| 1000 | + $allowed = [ '100', '200', '300', '400', '500', '600', '700', '800', '900', 'normal', 'bold', 'bolder', 'lighter' ]; | |
| 1001 | + return in_array( $val, $allowed, true ) ? $val : ''; | |
| 1002 | + } | |
| 1003 | + | |
| 1004 | + /** | |
| 1005 | + * Validate a stored value against an allowlist of CSS keywords. | |
| 1006 | + * | |
| 1007 | + * @param mixed $val | |
| 1008 | + * @param array<string> $allowed | |
| 1009 | + */ | |
| 1010 | + private static function css_keyword( $val, array $allowed ): string { | |
| 1011 | + $val = is_string( $val ) ? strtolower( trim( $val ) ) : ''; | |
| 1012 | + return in_array( $val, $allowed, true ) ? $val : ''; | |
| 1013 | + } | |
| 1014 | + | |
| 1015 | + /** | |
| 1016 | + * Extract a prefixed typography set (e.g. 'title_font_family') back into the | |
| 1017 | + * base keys the typography helpers expect ('font_family'). Lets one element | |
| 1018 | + * carry more than one independent typography set (see the Description widget, | |
| 1019 | + * which styles its title and body separately) while reusing the same helpers. | |
| 1020 | + * | |
| 1021 | + * @param array $settings Element settings. | |
| 1022 | + * @param string $prefix Key prefix to strip (e.g. 'title_'). | |
| 1023 | + * @return array<string, mixed> Base-keyed typography settings. | |
| 1024 | + */ | |
| 1025 | + private static function prefixed_typography( array $settings, string $prefix ): array { | |
| 1026 | + $keys = [ | |
| 1027 | + 'font_family', 'font_size', 'font_weight', 'text_transform', 'font_style', | |
| 1028 | + 'text_decoration', 'line_height', 'letter_spacing', 'word_spacing', 'color', | |
| 1029 | + ]; | |
| 1030 | + $out = []; | |
| 1031 | + foreach ( $keys as $key ) { | |
| 1032 | + if ( isset( $settings[ $prefix . $key ] ) ) { | |
| 1033 | + $out[ $key ] = $settings[ $prefix . $key ]; | |
| 1034 | + } | |
| 1035 | + } | |
| 1036 | + return $out; | |
| 1037 | + } | |
| 1038 | + | |
| 1039 | + /** | |
| 1040 | + * Build the "common" typography CSS declarations (everything except color and | |
| 1041 | + * font-size, which callers handle per-element) from element settings. | |
| 1042 | + * Only user-set, valid values are included. | |
| 1043 | + * | |
| 1044 | + * @param array $settings | |
| 1045 | + * @return array<string, string> Map of css-property => value. | |
| 1046 | + */ | |
| 1047 | + private static function typography_common_decls( array $settings ): array { | |
| 1048 | + $decls = []; | |
| 1049 | + | |
| 1050 | + $family = self::css_font_family( $settings['font_family'] ?? '' ); | |
| 1051 | + if ( '' !== $family ) { | |
| 1052 | + $decls['font-family'] = $family; | |
| 1053 | + } | |
| 1054 | + $weight = self::css_font_weight( $settings['font_weight'] ?? '' ); | |
| 1055 | + if ( '' !== $weight ) { | |
| 1056 | + $decls['font-weight'] = $weight; | |
| 1057 | + } | |
| 1058 | + $style = self::css_font_style( $settings['font_style'] ?? '' ); | |
| 1059 | + if ( '' !== $style ) { | |
| 1060 | + $decls['font-style'] = $style; | |
| 1061 | + } | |
| 1062 | + $transform = self::css_keyword( $settings['text_transform'] ?? '', [ 'uppercase', 'lowercase', 'capitalize', 'none' ] ); | |
| 1063 | + if ( '' !== $transform ) { | |
| 1064 | + $decls['text-transform'] = $transform; | |
| 1065 | + } | |
| 1066 | + $decoration = self::css_keyword( $settings['text_decoration'] ?? '', [ 'underline', 'overline', 'line-through', 'none' ] ); | |
| 1067 | + if ( '' !== $decoration ) { | |
| 1068 | + $decls['text-decoration'] = $decoration; | |
| 1069 | + } | |
| 1070 | + $line_height = self::css_line_height( $settings['line_height'] ?? '' ); | |
| 1071 | + if ( '' !== $line_height ) { | |
| 1072 | + $decls['line-height'] = $line_height; | |
| 1073 | + } | |
| 1074 | + $letter = self::css_length( $settings['letter_spacing'] ?? '' ); | |
| 1075 | + if ( '' !== $letter ) { | |
| 1076 | + $decls['letter-spacing'] = $letter; | |
| 1077 | + } | |
| 1078 | + $word = self::css_length( $settings['word_spacing'] ?? '' ); | |
| 1079 | + if ( '' !== $word ) { | |
| 1080 | + $decls['word-spacing'] = $word; | |
| 1081 | + } | |
| 1082 | + | |
| 1083 | + return $decls; | |
| 1084 | + } | |
| 1085 | + | |
| 1086 | + /** | |
| 1087 | + * Flatten a declaration map into an inline-style string. Every declaration is | |
| 1088 | + * emitted with !important so user overrides win over template stylesheet rules. | |
| 1089 | + * | |
| 1090 | + * @param array<string, string> $decls | |
| 1091 | + */ | |
| 1092 | + private static function decls_to_style( array $decls ): string { | |
| 1093 | + $out = ''; | |
| 1094 | + foreach ( $decls as $prop => $value ) { | |
| 1095 | + $out .= $prop . ':' . $value . ' !important;'; | |
| 1096 | + } | |
| 1097 | + return $out; | |
| 1098 | + } | |
| 1099 | + | |
| 1100 | + /** | |
| 671 | 1101 | * Returns the global Better Payment currency code from plugin settings. |
| 672 | 1102 | */ |
| 673 | 1103 | private static function global_currency(): string { |
| 674 | 1104 | $code = DB::get_settings( 'better_payment_settings_general_general_currency' ); |
| @@ -713,28 +1143,11 @@ | ||
| 713 | 1143 | 'post_status' => 'publish', |
| 714 | 1144 | 'post_name' => $key, |
| 715 | 1145 | ] ); |
| 716 | 1146 | |
| 717 | - $meta = [ | |
| 718 | - 'bpc_goal_amount' => 10000, | |
| 719 | - 'bpc_color_primary' => $template['preview_color'] ?? '#6b63f6', | |
| 720 | - 'bpc_color_button' => '', | |
| 721 | - 'bpc_suggested_amounts' => [], | |
| 722 | - 'bpc_allow_custom_amount' => true, | |
| 723 | - 'bpc_minimum_amount' => '', | |
| 724 | - 'bpc_form_page_id' => 0, | |
| 725 | - 'bpc_status' => 'active', | |
| 726 | - 'bpc_template_key' => $key, | |
| 727 | - 'bpc_css_class' => '', | |
| 728 | - ]; | |
| 1147 | + $meta = self::template_preview_meta( $key, $template ); | |
| 1148 | + $stats = self::template_preview_stats(); | |
| 729 | 1149 | |
| 730 | - $stats = [ | |
| 731 | - 'total_raised' => 3750, | |
| 732 | - 'progress' => 37.5, | |
| 733 | - 'donor_count' => 42, | |
| 734 | - 'days_remaining' => 18, | |
| 735 | - ]; | |
| 736 | - | |
| 737 | 1150 | $theme_class = isset( $template['theme_class'] ) ? ' ' . sanitize_html_class( $template['theme_class'] ) : ''; |
| 738 | 1151 | |
| 739 | 1152 | ob_start(); |
| 740 | 1153 | ?> |
| @@ -749,9 +1162,9 @@ | ||
| 749 | 1162 | <div class="bp-campaign-column" |
| 750 | 1163 | style="<?php echo $col_style; ?>"> |
| 751 | 1164 | <?php |
| 752 | 1165 | foreach ( $column['elements'] as $element ) { |
| 753 | - echo self::render_element( $element, 0, $fake_post, $meta, $stats ); | |
| 1166 | + echo self::render_element( $element, 0, $fake_post, $meta, $stats, true ); | |
| 754 | 1167 | } |
| 755 | 1168 | ?> |
| 756 | 1169 | </div> |
| 757 | 1170 | <?php endforeach; ?> |
| @@ -761,8 +1174,93 @@ | ||
| 761 | 1174 | return ob_get_clean(); |
| 762 | 1175 | } |
| 763 | 1176 | |
| 764 | 1177 | /** |
| 1178 | + * Demo meta for a template preview — no campaign exists behind it. | |
| 1179 | + * | |
| 1180 | + * Shared by the fragment renderer (render_template_preview) and the full | |
| 1181 | + * document one (build_template_preview_document) so the two can never show a | |
| 1182 | + * template with different figures or a different accent colour. | |
| 1183 | + * | |
| 1184 | + * @param string $key Template key. | |
| 1185 | + * @param array $template Template definition from TemplateManager. | |
| 1186 | + * @return array | |
| 1187 | + */ | |
| 1188 | + private static function template_preview_meta( string $key, array $template ): array { | |
| 1189 | + return [ | |
| 1190 | + 'title' => $template['default_title'] ?? $template['label'] ?? 'Campaign Preview', | |
| 1191 | + 'bpc_goal_amount' => 10000, | |
| 1192 | + 'bpc_color_primary' => $template['preview_color'] ?? '#6b63f6', | |
| 1193 | + 'bpc_color_background' => '', | |
| 1194 | + 'bpc_suggested_amounts' => [], | |
| 1195 | + 'bpc_allow_custom_amount' => true, | |
| 1196 | + 'bpc_minimum_amount' => '', | |
| 1197 | + 'bpc_form_page_id' => 0, | |
| 1198 | + 'bpc_status' => 'active', | |
| 1199 | + 'bpc_template_key' => $key, | |
| 1200 | + 'bpc_css_class' => '', | |
| 1201 | + ]; | |
| 1202 | + } | |
| 1203 | + | |
| 1204 | + /** | |
| 1205 | + * Demo stats for a template preview. | |
| 1206 | + * | |
| 1207 | + * A template has no transactions, so the real figures are all zero — which | |
| 1208 | + * renders an empty progress bar and "$0.00 raised" on every card, i.e. the | |
| 1209 | + * one state that shows least about the design. These are obviously-illustrative | |
| 1210 | + * round numbers, never presented as a real campaign's record. | |
| 1211 | + * | |
| 1212 | + * @return array | |
| 1213 | + */ | |
| 1214 | + private static function template_preview_stats(): array { | |
| 1215 | + return [ | |
| 1216 | + 'total_raised' => 3750, | |
| 1217 | + 'progress' => 37.5, | |
| 1218 | + 'donor_count' => 42, | |
| 1219 | + 'days_remaining' => 18, | |
| 1220 | + ]; | |
| 1221 | + } | |
| 1222 | + | |
| 1223 | + /** | |
| 1224 | + * Build a full, standalone HTML document for one template — the picker's | |
| 1225 | + * "Preview" lightbox. | |
| 1226 | + * | |
| 1227 | + * This is the whole design, top to bottom, rendered by the same code the | |
| 1228 | + * frontend uses; the lightbox scrolls it. A static screenshot cannot do that | |
| 1229 | + * (it is a fixed crop of the top of the page) and drifts from the layout the | |
| 1230 | + * moment a template is redesigned. | |
| 1231 | + * | |
| 1232 | + * Only a template with a layout of its own previews live. A locked Pro card | |
| 1233 | + * (ProTemplateCatalog, free install only) carries `columns => []`, and Lite | |
| 1234 | + * ships no copy of Pro's layouts, so it returns '' and the lightbox shows the | |
| 1235 | + * card's screenshot — the builder does not even request it. With Pro active | |
| 1236 | + * the same key resolves to Pro's real template and renders like any other. | |
| 1237 | + * Falling through to `default_layout()` instead would advertise Pro's design | |
| 1238 | + * as a generic campaign. | |
| 1239 | + * | |
| 1240 | + * @param string $key Template key from TemplateManager. | |
| 1241 | + * @return string Full HTML document, or '' when the key is unknown or has no layout. | |
| 1242 | + */ | |
| 1243 | + public static function build_template_preview_document( string $key ): string { | |
| 1244 | + $templates = TemplateManager::get_all(); | |
| 1245 | + if ( empty( $templates[ $key ]['columns'] ) ) { | |
| 1246 | + return ''; | |
| 1247 | + } | |
| 1248 | + | |
| 1249 | + $template = $templates[ $key ]; | |
| 1250 | + | |
| 1251 | + return self::build_preview_document( | |
| 1252 | + [ | |
| 1253 | + 'layout' => $template['layout'] ?? '1-column', | |
| 1254 | + 'columns' => $template['columns'], | |
| 1255 | + ], | |
| 1256 | + self::template_preview_meta( $key, $template ), | |
| 1257 | + 0, | |
| 1258 | + self::template_preview_stats() | |
| 1259 | + ); | |
| 1260 | + } | |
| 1261 | + | |
| 1262 | + /** | |
| 765 | 1263 | * Build a full HTML preview document for the builder's live-preview iframe. |
| 766 | 1264 | * |
| 767 | 1265 | * Each element is wrapped in a <div data-bp-element-id> so the JS overlay |
| 768 | 1266 | * can measure positions and wire up hover/drag interactions. Works for both |
| @@ -767,17 +1265,23 @@ | ||
| 767 | 1265 | * Each element is wrapped in a <div data-bp-element-id> so the JS overlay |
| 768 | 1266 | * can measure positions and wire up hover/drag interactions. Works for both |
| 769 | 1267 | * new unsaved campaigns (campaign_id = 0) and existing ones. |
| 770 | 1268 | * |
| 771 | - * @param array $layout_data Builder layout: { layout, columns }. | |
| 772 | - * @param array $meta_input Campaign meta from the builder store (includes 'title'). | |
| 773 | - * @param int $campaign_id 0 for new campaigns; real ID to pull live stats. | |
| 1269 | + * @param array $layout_data Builder layout: { layout, columns }. | |
| 1270 | + * @param array $meta_input Campaign meta from the builder store (includes 'title'). | |
| 1271 | + * @param int $campaign_id 0 for new campaigns; real ID to pull live stats. | |
| 1272 | + * @param array $stats_override Demo figures merged over the computed stats, for | |
| 1273 | + * previews that have no campaign behind them at all | |
| 1274 | + * (the template picker). Applied LAST, after the | |
| 1275 | + * goal/end-date recomputations below, so it wins — | |
| 1276 | + * that is the whole point of an override. | |
| 774 | 1277 | * @return string Full HTML document string. |
| 775 | 1278 | */ |
| 776 | 1279 | public static function build_preview_document( |
| 777 | 1280 | array $layout_data, |
| 778 | 1281 | array $meta_input, |
| 779 | - int $campaign_id = 0 | |
| 1282 | + int $campaign_id = 0, | |
| 1283 | + array $stats_override = [] | |
| 780 | 1284 | ): string { |
| 781 | 1285 | // ── Post + stats ────────────────────────────────────────────────────── |
| 782 | 1286 | $post = null; |
| 783 | 1287 | $stats = null; |
| @@ -823,13 +1327,17 @@ | ||
| 823 | 1327 | if ( $preview_goal > 0 ) { |
| 824 | 1328 | $stats['progress'] = min( 100.0, round( ( $stats['total_raised'] / $preview_goal ) * 100, 1 ) ); |
| 825 | 1329 | } |
| 826 | 1330 | |
| 1331 | + if ( ! empty( $stats_override ) ) { | |
| 1332 | + $stats = array_merge( $stats, $stats_override ); | |
| 1333 | + } | |
| 1334 | + | |
| 827 | 1335 | // ── Meta ───────────────────────────────────────────────────────────── |
| 828 | 1336 | $meta_defaults = [ |
| 829 | 1337 | 'bpc_goal_amount' => 0, |
| 830 | 1338 | 'bpc_color_primary' => '#6b63f6', |
| 831 | - 'bpc_color_button' => '', | |
| 1339 | + 'bpc_color_background' => '', | |
| 832 | 1340 | 'bpc_suggested_amounts' => [], |
| 833 | 1341 | 'bpc_allow_custom_amount' => true, |
| 834 | 1342 | 'bpc_minimum_amount' => '', |
| 835 | 1343 | 'bpc_form_page_id' => 0, |
| @@ -883,13 +1391,51 @@ | ||
| 883 | 1391 | ?> |
| 884 | 1392 | <div class="<?php echo esc_attr( $col_class ); ?>" |
| 885 | 1393 | data-bp-column-id="<?php echo esc_attr( $column['id'] ); ?>" |
| 886 | 1394 | style="<?php echo esc_attr( $col_style ); ?>"> |
| 887 | - <?php foreach ( $column['elements'] as $element ) : ?> | |
| 1395 | + <?php | |
| 1396 | + $locked_types = self::pro_locked_types(); | |
| 1397 | + foreach ( $column['elements'] as $element ) : | |
| 1398 | + $el_type = $element['type'] ?? ''; | |
| 1399 | + ?> | |
| 888 | 1400 | <div data-bp-element-id="<?php echo esc_attr( $element['id'] ?? '' ); ?>" |
| 889 | 1401 | data-bp-column-id="<?php echo esc_attr( $column['id'] ); ?>" |
| 890 | 1402 | class="bp-builder-el-wrap"> |
| 891 | - <?php echo self::render_element( $element, $campaign_id, $post, $meta, $stats ); ?> | |
| 1403 | + <?php | |
| 1404 | + // Pro elements — either dropped from the palette by a free | |
| 1405 | + // user, or left behind by a campaign built while Pro was | |
| 1406 | + // active. Either way Pro's renderer is not listening, so | |
| 1407 | + // rendering them normally yields nothing: no markup, no | |
| 1408 | + // hotspot, an element that cannot be selected or removed. | |
| 1409 | + // | |
| 1410 | + // Show a mock preview instead so the element is visible, | |
| 1411 | + // selectable, and demonstrates what Pro would do with the | |
| 1412 | + // upgrade banner shown in the sidebar. | |
| 1413 | + // | |
| 1414 | + // This is the ONLY call site — the public render path has no | |
| 1415 | + // reference to ProElementPreview, and ProElementPreview::render() | |
| 1416 | + // re-checks the preview flag itself. Its data is fabricated; | |
| 1417 | + // on a live campaign page it would be a lie about who donated. | |
| 1418 | + if ( isset( $locked_types[ $el_type ] ) ) { | |
| 1419 | + // Only our own three types have a mock. Any other | |
| 1420 | + // `pro`-flagged element (a third party's, via the | |
| 1421 | + // campaign_elements filter) still needs *something* | |
| 1422 | + // clickable, so it falls back to the generic banner. | |
| 1423 | + $mock = ProElementPreview::render( | |
| 1424 | + $el_type, | |
| 1425 | + isset( $element['settings'] ) && is_array( $element['settings'] ) ? $element['settings'] : [], | |
| 1426 | + true | |
| 1427 | + ); | |
| 1428 | + | |
| 1429 | + if ( '' === $mock ) { | |
| 1430 | + $mock = self::pro_locked_placeholder( $locked_types[ $el_type ] ); | |
| 1431 | + } | |
| 1432 | + | |
| 1433 | + echo $mock; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 1434 | + } else { | |
| 1435 | + echo self::render_element( $element, $campaign_id, $post, $meta, $stats, true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 1436 | + } | |
| 1437 | + ?> | |
| 892 | 1438 | </div> |
| 893 | 1439 | <?php endforeach; ?> |
| 894 | 1440 | </div> |
| 895 | 1441 | <?php endforeach; ?> |
| @@ -909,8 +1455,16 @@ | ||
| 909 | 1455 | $extra_link = file_exists( BETTER_PAYMENT_ASSETS_PATH . '/css/fundraising-campaign.min.css' ) |
| 910 | 1456 | ? '<link rel="stylesheet" href="' . esc_url( $fundraising_css ) . '?v=' . $v . '">' . "\n" |
| 911 | 1457 | : ''; |
| 912 | 1458 | |
| 1459 | + // Template theme CSS from an add-on — Pro links its themes through | |
| 1460 | + // `better_payment/campaign/preview_stylesheets`. A free install links no | |
| 1461 | + // Pro design. See Campaign\CampaignStyles. | |
| 1462 | + $theme_links = ''; | |
| 1463 | + foreach ( \Better_Payment\Lite\Campaign\CampaignStyles::preview_stylesheets() as $theme_css ) { | |
| 1464 | + $theme_links .= '<link rel="stylesheet" href="' . esc_url( $theme_css ) . '">' . "\n"; | |
| 1465 | + } | |
| 1466 | + | |
| 913 | 1467 | return '<!DOCTYPE html>' . "\n" |
| 914 | 1468 | . '<html>' . "\n" |
| 915 | 1469 | . '<head>' . "\n" |
| 916 | 1470 | . '<meta charset="utf-8">' . "\n" |
| @@ -916,8 +1470,9 @@ | ||
| 916 | 1470 | . '<meta charset="utf-8">' . "\n" |
| 917 | 1471 | . '<meta name="viewport" content="width=device-width, initial-scale=1">' . "\n" |
| 918 | 1472 | . '<base href="' . esc_url( home_url( '/' ) ) . '">' . "\n" |
| 919 | 1473 | . '<link rel="stylesheet" href="' . esc_url( $display_css ) . '?v=' . $v . '">' . "\n" |
| 1474 | + . $theme_links | |
| 920 | 1475 | . $extra_link |
| 921 | 1476 | . '<style>' . "\n" |
| 922 | 1477 | . '*, *::before, *::after { box-sizing: border-box; }' . "\n" |
| 923 | 1478 | . 'html, body { margin: 0; padding: 0; background: #fff; }' . "\n" |
| @@ -930,8 +1485,24 @@ | ||
| 930 | 1485 | . ' height: 100%;' . "\n" |
| 931 | 1486 | . ' display: flex;' . "\n" |
| 932 | 1487 | . ' flex-direction: column;' . "\n" |
| 933 | 1488 | . '}' . "\n" |
| 1489 | + // The preview is read-only: no link/button/form control should be | |
| 1490 | + // clickable (e.g. the Donate Now button must not navigate away). | |
| 1491 | + // pointer-events:none only blocks pointer interaction — page scrolling | |
| 1492 | + // still works. This document is preview-only (the live frontend uses | |
| 1493 | + // render_campaign() directly), so real pages are unaffected. | |
| 1494 | + . 'a, button, input, select, textarea, [role="button"] {' . "\n" | |
| 1495 | + . ' pointer-events: none !important;' . "\n" | |
| 1496 | + . ' cursor: default !important;' . "\n" | |
| 1497 | + . '}' . "\n" | |
| 1498 | + // Pro-locked element placeholder (editor preview only). | |
| 1499 | + . '.bp-pro-locked { position: relative; border: 1px dashed #f0a020; background: #fff8ee; border-radius: 8px; padding: 22px 18px; text-align: center; }' . "\n" | |
| 1500 | + . '.bp-pro-locked__badge { display: inline-block; font-size: 10px; font-weight: 700; letter-spacing: 0.5px; color: #fff; background: linear-gradient(135deg, #f6a821, #ec6a2b); padding: 3px 8px; border-radius: 20px; margin-bottom: 8px; }' . "\n" | |
| 1501 | + . '.bp-pro-locked__icon { color: #d9821a; }' . "\n" | |
| 1502 | + . '.bp-pro-locked__icon .dashicons { width: 26px; height: 26px; font-size: 26px; }' . "\n" | |
| 1503 | + . '.bp-pro-locked__title { font-size: 15px; font-weight: 700; color: #1a1a2e; margin: 6px 0 4px; }' . "\n" | |
| 1504 | + . '.bp-pro-locked__text { font-size: 12px; color: #7a6a52; margin: 0; line-height: 1.5; }' . "\n" | |
| 934 | 1505 | . '</style>' . "\n" |
| 935 | 1506 | . '</head>' . "\n" |
| 936 | 1507 | . '<body>' . "\n" |
| 937 | 1508 | . preg_replace( '/<script\b[^>]*>.*?<\/script>/is', '', $campaign_html ) . "\n" |
| @@ -936,8 +1507,86 @@ | ||
| 936 | 1507 | . '<body>' . "\n" |
| 937 | 1508 | . preg_replace( '/<script\b[^>]*>.*?<\/script>/is', '', $campaign_html ) . "\n" |
| 938 | 1509 | . '</body>' . "\n" |
| 939 | 1510 | . '</html>'; |
| 1511 | + } | |
| 1512 | + | |
| 1513 | + /** | |
| 1514 | + * Per-request memo for pro_locked_types(). A property rather than a | |
| 1515 | + * function-local static so tests can reset it. | |
| 1516 | + * | |
| 1517 | + * @var array<string, array>|null | |
| 1518 | + */ | |
| 1519 | + private static $pro_locked_types_cache = null; | |
| 1520 | + | |
| 1521 | + /** | |
| 1522 | + * Element types that are Pro-only AND currently locked (Pro inactive). | |
| 1523 | + * | |
| 1524 | + * Returns a map of type => schema (label/icon) for elements that carry the | |
| 1525 | + * `pro` flag in the filtered registry while `better_payment/pro_enabled` is | |
| 1526 | + * false. With Pro active the map is empty and nothing is locked. Result is | |
| 1527 | + * cached per request. | |
| 1528 | + * | |
| 1529 | + * @return array<string, array> | |
| 1530 | + */ | |
| 1531 | + private static function pro_locked_types(): array { | |
| 1532 | + if ( null !== self::$pro_locked_types_cache ) { | |
| 1533 | + return self::$pro_locked_types_cache; | |
| 1534 | + } | |
| 1535 | + | |
| 1536 | + self::$pro_locked_types_cache = []; | |
| 1537 | + | |
| 1538 | + if ( apply_filters( 'better_payment/pro_enabled', false ) ) { | |
| 1539 | + return self::$pro_locked_types_cache; | |
| 1540 | + } | |
| 1541 | + | |
| 1542 | + foreach ( ElementRegistry::get_all() as $type => $schema ) { | |
| 1543 | + if ( ! empty( $schema['pro'] ) ) { | |
| 1544 | + self::$pro_locked_types_cache[ $type ] = $schema; | |
| 1545 | + } | |
| 1546 | + } | |
| 1547 | + | |
| 1548 | + return self::$pro_locked_types_cache; | |
| 1549 | + } | |
| 1550 | + | |
| 1551 | + /** | |
| 1552 | + * Generic builder-only "PRO" banner for a locked element that has no mock. | |
| 1553 | + * | |
| 1554 | + * Lite's own three Pro elements always get a ProElementPreview mock, so this | |
| 1555 | + * is reached only by some other `pro`-flagged type — a third party's, added | |
| 1556 | + * through `better_payment/campaign_elements`, or a future Pro element whose | |
| 1557 | + * mock has not been written. It is lock chrome, not Pro functionality: it | |
| 1558 | + * renders a label and an upgrade line, nothing of the element itself. Without | |
| 1559 | + * it the element's canvas wrapper would be empty and zero-height, so the canvas | |
| 1560 | + * could place no hotspot and the element could not be selected or deleted. | |
| 1561 | + * Only build_preview_document() calls it, only while Pro is inactive | |
| 1562 | + * (pro_locked_types() is empty otherwise). Never emitted on the public frontend. | |
| 1563 | + * | |
| 1564 | + * @param array $schema Element schema (label, icon). | |
| 1565 | + * @return string | |
| 1566 | + */ | |
| 1567 | + private static function pro_locked_placeholder( array $schema ): string { | |
| 1568 | + $label = ! empty( $schema['label'] ) ? $schema['label'] : __( 'Pro Element', 'better-payment' ); | |
| 1569 | + $icon = ! empty( $schema['icon'] ) ? $schema['icon'] : 'lock'; | |
| 1570 | + | |
| 1571 | + ob_start(); | |
| 1572 | + ?> | |
| 1573 | + <div class="bp-pro-locked"> | |
| 1574 | + <span class="bp-pro-locked__badge"><?php esc_html_e( 'PRO', 'better-payment' ); ?></span> | |
| 1575 | + <div class="bp-pro-locked__icon"><span class="dashicons dashicons-<?php echo esc_attr( $icon ); ?>"></span></div> | |
| 1576 | + <div class="bp-pro-locked__title"><?php echo esc_html( $label ); ?></div> | |
| 1577 | + <p class="bp-pro-locked__text"> | |
| 1578 | + <?php | |
| 1579 | + printf( | |
| 1580 | + /* translators: %s: element name, e.g. "Donors Wall". */ | |
| 1581 | + esc_html__( '%s is a Better Payment Pro element. Activate Pro to display it on your campaign.', 'better-payment' ), | |
| 1582 | + esc_html( $label ) | |
| 1583 | + ); | |
| 1584 | + ?> | |
| 1585 | + </p> | |
| 1586 | + </div> | |
| 1587 | + <?php | |
| 1588 | + return (string) ob_get_clean(); | |
| 940 | 1589 | } |
| 941 | 1590 | |
| 942 | 1591 | private static function default_layout(): array { |
| 943 | 1592 | return [ |