| @@ -35,8 +35,24 @@ | ||
| 35 | 35 | */ |
| 36 | 36 | public const VALIDATION_MESSAGES_OPTION_KEY = 'validation_messages'; |
| 37 | 37 | |
| 38 | 38 | /** |
| 39 | + * Canonical stored values for a checkbox field. | |
| 40 | + * | |
| 41 | + * Deliberately untranslated: the value is persisted to donation_data, read | |
| 42 | + * back by the entry screen, the abilities runtime and the CSV export, and can | |
| 43 | + * be re-imported on another site. Display layers translate it on read via | |
| 44 | + * Helper::format_checkbox_field_value(); the export keeps the canonical token | |
| 45 | + * so the column stays comparable across locales. | |
| 46 | + * | |
| 47 | + * @since 1.5.1 | |
| 48 | + */ | |
| 49 | + public const CHECKBOX_VALUES = [ | |
| 50 | + 'yes' => 'Yes', | |
| 51 | + 'no' => 'No', | |
| 52 | + ]; | |
| 53 | + | |
| 54 | + /** | |
| 39 | 55 | * Field blocks whose values participate in field-level validation. |
| 40 | 56 | * |
| 41 | 57 | * @since 1.1.0 |
| 42 | 58 | */ |
| @@ -43,11 +59,13 @@ | ||
| 43 | 59 | public const VALIDATABLE_BLOCKS = [ |
| 44 | 60 | 'suredonation/input', |
| 45 | 61 | 'suredonation/email', |
| 46 | 62 | 'suredonation/number', |
| 63 | + 'suredonation/checkbox', | |
| 47 | 64 | 'suredonation/dropdown', |
| 48 | 65 | 'suredonation/phone', |
| 49 | 66 | 'suredonation/url', |
| 67 | + 'suredonation/donor-comment', | |
| 50 | 68 | ]; |
| 51 | 69 | |
| 52 | 70 | /** |
| 53 | 71 | * Add block configuration for form fields. |
| @@ -170,8 +188,11 @@ | ||
| 170 | 188 | break; |
| 171 | 189 | case 'suredonation/email': |
| 172 | 190 | $processed_config = self::process_email_block( $block['attrs'] ); |
| 173 | 191 | break; |
| 192 | + case 'suredonation/checkbox': | |
| 193 | + $processed_config = self::process_checkbox_block( $block['attrs'] ); | |
| 194 | + break; | |
| 174 | 195 | case 'suredonation/dropdown': |
| 175 | 196 | $processed_config = self::process_dropdown_block( $block['attrs'] ); |
| 176 | 197 | break; |
| 177 | 198 | case 'suredonation/phone': |
| @@ -179,8 +200,11 @@ | ||
| 179 | 200 | break; |
| 180 | 201 | case 'suredonation/url': |
| 181 | 202 | $processed_config = self::process_url_block( $block['attrs'] ); |
| 182 | 203 | break; |
| 204 | + case 'suredonation/donor-comment': | |
| 205 | + $processed_config = self::process_donor_comment_block( $block['attrs'] ); | |
| 206 | + break; | |
| 183 | 207 | } |
| 184 | 208 | |
| 185 | 209 | /** |
| 186 | 210 | * Filter the stored validation config for a field block. |
| @@ -190,8 +214,13 @@ | ||
| 190 | 214 | * persisted on save and picked up by validate_form_data(). Return a |
| 191 | 215 | * non-empty array (including at least a 'required' flag plus any rule |
| 192 | 216 | * values the validator needs) to store it under the block id. |
| 193 | 217 | * |
| 218 | + * Set 'is_checkbox' => true for a consent-style boolean field so the | |
| 219 | + * submission handler stores its value as the canonical Yes/No token | |
| 220 | + * (see CHECKBOX_VALUES) and keeps the unticked state on the record | |
| 221 | + * instead of dropping it as an empty value. | |
| 222 | + * | |
| 194 | 223 | * @since 1.1.0 |
| 195 | 224 | * @param array<string, mixed>|null $processed_config Config from core (null when unhandled). |
| 196 | 225 | * @param string $block_name Block name. |
| 197 | 226 | * @param array<string, mixed> $attrs Block attributes. |
| @@ -216,12 +245,16 @@ | ||
| 216 | 245 | /** |
| 217 | 246 | * Process payment block configuration. |
| 218 | 247 | * |
| 219 | 248 | * Extracts payment-related settings that are needed for server-side validation: |
| 249 | + * - payment_type: 'one-time', 'subscription' or 'both' | |
| 220 | 250 | * - amount_type: 'fixed' or 'variable' |
| 221 | 251 | * - fixed_amount: The configured fixed amount |
| 222 | 252 | * - minimum_amount: The minimum allowed amount for variable amounts |
| 223 | 253 | * - variable_amount_field: The slug of the field providing the variable amount |
| 254 | + * - one_time / subscription: per-choice amount configs, 'both' mode only | |
| 255 | + * - subscription_interval / subscription_billing_cycles: billing cadence, when a | |
| 256 | + * subscription path exists | |
| 224 | 257 | * |
| 225 | 258 | * @param array<mixed> $attrs Block attributes. |
| 226 | 259 | * @param array<mixed> $blocks All blocks in the form. |
| 227 | 260 | * @return array<string, mixed> Processed payment configuration. |
| @@ -229,48 +262,103 @@ | ||
| 229 | 262 | */ |
| 230 | 263 | private static function process_payment_block( $attrs, $blocks ) { |
| 231 | 264 | $payment_config = []; |
| 232 | 265 | |
| 233 | - // Extract payment type (one-time or subscription). | |
| 266 | + // Extract payment type (one-time, subscription, or both). | |
| 234 | 267 | // Default to 'one-time' if not set (Gutenberg may not save default values). |
| 235 | 268 | $payment_config['payment_type'] = isset( $attrs['paymentType'] ) && is_string( $attrs['paymentType'] ) |
| 236 | 269 | ? sanitize_text_field( $attrs['paymentType'] ) |
| 237 | 270 | : 'one-time'; |
| 238 | 271 | |
| 239 | - // Extract amount type (fixed or variable). | |
| 240 | - // IMPORTANT: Always store this - Gutenberg may not save attributes that match defaults. | |
| 241 | - // Default to 'fixed' which is the block.json default. | |
| 242 | - $payment_config['amount_type'] = isset( $attrs['amountType'] ) && is_string( $attrs['amountType'] ) | |
| 243 | - ? sanitize_text_field( $attrs['amountType'] ) | |
| 244 | - : 'fixed'; | |
| 272 | + // Shared amount configuration. Kept at the top level for every payment type, | |
| 273 | + // including 'both', so blocks saved before dual-mode support — and any code | |
| 274 | + // still reading the flat keys — behave exactly as before. | |
| 275 | + $payment_config = array_merge( $payment_config, self::build_amount_config( $attrs, $blocks ) ); | |
| 245 | 276 | |
| 246 | - // Extract configured fixed amount. | |
| 247 | - // Default to 10.00 to match block.json default. | |
| 248 | - $payment_config['fixed_amount'] = isset( $attrs['fixedAmount'] ) | |
| 249 | - ? floatval( $attrs['fixedAmount'] ) | |
| 250 | - : 10.00; | |
| 277 | + // In 'both' mode each choice carries its own amount configuration. Store them | |
| 278 | + // as separate sub-configs so validation can check the submitted amount against | |
| 279 | + // the mode the donor actually selected rather than a single shared amount. | |
| 280 | + if ( 'both' === $payment_config['payment_type'] ) { | |
| 281 | + $payment_config['one_time'] = self::build_amount_config( $attrs, $blocks, 'oneTime' ); | |
| 282 | + $payment_config['subscription'] = self::build_amount_config( $attrs, $blocks, 'subscription' ); | |
| 283 | + } | |
| 251 | 284 | |
| 252 | - // Extract minimum amount for variable amounts. | |
| 253 | - // Defaults to 0 (no minimum) — only enforced if the block setting specifies one. | |
| 254 | - $payment_config['minimum_amount'] = isset( $attrs['minimumAmount'] ) | |
| 255 | - ? floatval( $attrs['minimumAmount'] ) | |
| 256 | - : 0.0; | |
| 285 | + // Persist the billing cadence for any form with a subscription path. The admin | |
| 286 | + // picks these in the editor, so the stored values are the source of truth on | |
| 287 | + // submit — a tampered interval/cycles in the request cannot redirect the | |
| 288 | + // gateway to a different cadence. | |
| 289 | + // | |
| 290 | + // Stored UNCONDITIONALLY with PHP-side defaults (not gated on | |
| 291 | + // isset( subscriptionPlan )), exactly like build_amount_config() below: | |
| 292 | + // block.json's subscriptionPlan default is a fully-populated object, so | |
| 293 | + // Gutenberg omits the attribute whenever the admin accepts the defaults | |
| 294 | + // (Monthly / Ongoing / default name). Gating on it would leave the cadence | |
| 295 | + // unstored for that common case, get_subscription_cadence() would return | |
| 296 | + // empty, and the submit path would fall back to the request-supplied cadence. | |
| 297 | + if ( in_array( $payment_config['payment_type'], [ 'subscription', 'both' ], true ) ) { | |
| 298 | + $cadence = self::derive_subscription_cadence_from_attrs( $attrs ); | |
| 299 | + $payment_config['subscription_interval'] = $cadence['subscription_interval']; | |
| 300 | + $payment_config['subscription_billing_cycles'] = $cadence['subscription_billing_cycles']; | |
| 301 | + } | |
| 257 | 302 | |
| 258 | - // Extract variable amount field reference. | |
| 259 | - if ( isset( $attrs['variableAmountField'] ) ) { | |
| 260 | - $variable_amount_slug = sanitize_text_field( $attrs['variableAmountField'] ); | |
| 261 | - $payment_config['variable_amount_field'] = $variable_amount_slug; | |
| 303 | + return $payment_config; | |
| 304 | + } | |
| 262 | 305 | |
| 263 | - // Find and add the block name from which the variable amount field comes from. | |
| 264 | - if ( ! empty( $variable_amount_slug ) && is_array( $blocks ) ) { | |
| 265 | - $block_name = self::find_block_name_by_slug( $blocks, $variable_amount_slug ); | |
| 266 | - if ( $block_name ) { | |
| 267 | - $payment_config['variable_amount_field_block_name'] = $block_name; | |
| 268 | - } | |
| 306 | + /** | |
| 307 | + * Build one amount configuration (type, fixed, minimum, variable field) from a | |
| 308 | + * set of block attributes. | |
| 309 | + * | |
| 310 | + * Single-mode blocks use the unprefixed attributes (`amountType`, `fixedAmount`, | |
| 311 | + * …); 'both' mode stores an independent configuration per choice under the | |
| 312 | + * `oneTime`/`subscription` attribute prefixes. Defaults match block.json, because | |
| 313 | + * Gutenberg omits attributes whose value equals the default. | |
| 314 | + * | |
| 315 | + * @param array<mixed> $attrs Block attributes. | |
| 316 | + * @param array<mixed> $blocks All blocks in the form. | |
| 317 | + * @param string $prefix Attribute prefix ('' for the shared config, 'oneTime' or 'subscription'). | |
| 318 | + * @return array<string, mixed> Amount configuration. | |
| 319 | + * @since 1.5.1 | |
| 320 | + */ | |
| 321 | + private static function build_amount_config( $attrs, $blocks, $prefix = '' ) { | |
| 322 | + // Maps a suffix onto the prefixed attribute name: an empty prefix gives | |
| 323 | + // "amountType", the oneTime prefix gives "oneTimeAmountType". | |
| 324 | + $attr_key = static function ( $name ) use ( $prefix ) { | |
| 325 | + return '' === $prefix ? lcfirst( $name ) : $prefix . $name; | |
| 326 | + }; | |
| 327 | + | |
| 328 | + $amount_type_key = $attr_key( 'AmountType' ); | |
| 329 | + $fixed_key = $attr_key( 'FixedAmount' ); | |
| 330 | + $minimum_key = $attr_key( 'MinimumAmount' ); | |
| 331 | + $variable_key = $attr_key( 'VariableAmountField' ); | |
| 332 | + | |
| 333 | + $config = [ | |
| 334 | + 'amount_type' => isset( $attrs[ $amount_type_key ] ) && is_string( $attrs[ $amount_type_key ] ) | |
| 335 | + ? sanitize_text_field( $attrs[ $amount_type_key ] ) | |
| 336 | + : 'fixed', | |
| 337 | + 'fixed_amount' => isset( $attrs[ $fixed_key ] ) ? floatval( Helper::get_string_value( $attrs[ $fixed_key ] ) ) : 10.00, | |
| 338 | + // Defaults to 0 (no minimum) — only enforced if the block setting specifies one. | |
| 339 | + 'minimum_amount' => isset( $attrs[ $minimum_key ] ) ? floatval( Helper::get_string_value( $attrs[ $minimum_key ] ) ) : 0.0, | |
| 340 | + ]; | |
| 341 | + | |
| 342 | + // Stored unconditionally (empty string when unset) so absence is | |
| 343 | + // unambiguous: a 'variable' choice whose field was never picked reads as | |
| 344 | + // '' here, which validate_dynamic_amount_field() must reject rather than | |
| 345 | + // wave through. Gutenberg omits the attribute while it equals its '' | |
| 346 | + // default, so keying on isset() alone would hide that misconfiguration. | |
| 347 | + $variable_amount_slug = isset( $attrs[ $variable_key ] ) | |
| 348 | + ? sanitize_text_field( Helper::get_string_value( $attrs[ $variable_key ] ) ) | |
| 349 | + : ''; | |
| 350 | + $config['variable_amount_field'] = $variable_amount_slug; | |
| 351 | + | |
| 352 | + // Find and add the block name from which the variable amount field comes from. | |
| 353 | + if ( '' !== $variable_amount_slug && is_array( $blocks ) ) { | |
| 354 | + $block_name = self::find_block_name_by_slug( $blocks, $variable_amount_slug ); | |
| 355 | + if ( $block_name ) { | |
| 356 | + $config['variable_amount_field_block_name'] = $block_name; | |
| 269 | 357 | } |
| 270 | 358 | } |
| 271 | 359 | |
| 272 | - return $payment_config; | |
| 360 | + return $config; | |
| 273 | 361 | } |
| 274 | 362 | |
| 275 | 363 | /** |
| 276 | 364 | * Find block name by slug recursively. |
| @@ -324,24 +412,115 @@ | ||
| 324 | 412 | if ( ! ( $post instanceof \WP_Post ) || empty( $post->post_content ) ) { |
| 325 | 413 | return []; |
| 326 | 414 | } |
| 327 | 415 | |
| 328 | - $payment_attrs = self::find_payment_block_attrs( parse_blocks( $post->post_content ) ); | |
| 329 | - if ( empty( $payment_attrs ) ) { | |
| 330 | - return []; | |
| 416 | + $blocks = parse_blocks( $post->post_content ); | |
| 417 | + $slugs = []; | |
| 418 | + | |
| 419 | + $payment_attrs = self::find_payment_block_attrs( $blocks ); | |
| 420 | + if ( ! empty( $payment_attrs ) ) { | |
| 421 | + foreach ( [ 'customerNameField', 'customerEmailField', 'customerPhoneField', 'variableAmountField' ] as $attr ) { | |
| 422 | + if ( isset( $payment_attrs[ $attr ] ) && is_string( $payment_attrs[ $attr ] ) ) { | |
| 423 | + $slug = sanitize_text_field( $payment_attrs[ $attr ] ); | |
| 424 | + if ( '' !== $slug ) { | |
| 425 | + $slugs[] = $slug; | |
| 426 | + } | |
| 427 | + } | |
| 428 | + } | |
| 331 | 429 | } |
| 332 | 430 | |
| 333 | - $slugs = []; | |
| 334 | - foreach ( [ 'customerNameField', 'customerEmailField', 'customerPhoneField', 'variableAmountField' ] as $attr ) { | |
| 335 | - if ( isset( $payment_attrs[ $attr ] ) && is_string( $payment_attrs[ $attr ] ) ) { | |
| 336 | - $slug = sanitize_text_field( $payment_attrs[ $attr ] ); | |
| 337 | - if ( '' !== $slug ) { | |
| 338 | - $slugs[] = $slug; | |
| 431 | + // The donor comment also lives in its own column, so it is excluded from | |
| 432 | + // the additional set for the same reason. Unlike the fields above it is not | |
| 433 | + // mapped on the payment block — the presence of the block is the mapping — | |
| 434 | + // so it is resolved from the already-parsed tree rather than through | |
| 435 | + // get_donor_comment_slug(), which would parse the form a second time. | |
| 436 | + $comment_slug = self::find_slug_by_block_name( $blocks, 'suredonation/donor-comment' ); | |
| 437 | + if ( is_string( $comment_slug ) && '' !== $comment_slug ) { | |
| 438 | + $slugs[] = sanitize_text_field( $comment_slug ); | |
| 439 | + } | |
| 440 | + | |
| 441 | + return array_values( array_unique( $slugs ) ); | |
| 442 | + } | |
| 443 | + | |
| 444 | + /** | |
| 445 | + * Resolve the slug of the form's Donor Comment field. | |
| 446 | + * | |
| 447 | + * Unlike the donor phone — which is mapped through a picker on the payment | |
| 448 | + * block — the Donor Comment field is its own block, so the block's presence | |
| 449 | + * in the saved form *is* the mapping. Returning the slug lets the submission | |
| 450 | + * handlers read the already-validated value out of the submitted field set | |
| 451 | + * and store it in the dedicated donor_comment column, rather than trusting a | |
| 452 | + * separate client-supplied key. A comment posted against a form that has no | |
| 453 | + * Donor Comment block is therefore ignored, matching how | |
| 454 | + * Payment_Helper::get_submitted_is_anonymous() derives the anonymity option | |
| 455 | + * from the saved form. | |
| 456 | + * | |
| 457 | + * Only one field can feed the single column: when a form somehow contains | |
| 458 | + * more than one block (the editor warns against it), the first in document | |
| 459 | + * order wins. | |
| 460 | + * | |
| 461 | + * @since 1.6.0 | |
| 462 | + * @param int $form_id The donation form post ID. | |
| 463 | + * @return string The Donor Comment field slug, or '' when the form has none. | |
| 464 | + */ | |
| 465 | + public static function get_donor_comment_slug( $form_id ) { | |
| 466 | + $form_id = (int) $form_id; | |
| 467 | + if ( $form_id <= 0 || ! function_exists( 'parse_blocks' ) ) { | |
| 468 | + return ''; | |
| 469 | + } | |
| 470 | + | |
| 471 | + // form_id is attacker-chosen on a public endpoint, so confirm it really is | |
| 472 | + // a donation form before parsing its content — otherwise the request can | |
| 473 | + // aim a full block parse at any post in the database. | |
| 474 | + $post = get_post( $form_id ); | |
| 475 | + if ( ! ( $post instanceof \WP_Post ) | |
| 476 | + || \SureDonation\Inc\Post_Types\Donation_Form::POST_TYPE !== $post->post_type | |
| 477 | + || empty( $post->post_content ) ) { | |
| 478 | + return ''; | |
| 479 | + } | |
| 480 | + | |
| 481 | + $slug = self::find_slug_by_block_name( parse_blocks( $post->post_content ), 'suredonation/donor-comment' ); | |
| 482 | + | |
| 483 | + return null === $slug ? '' : sanitize_text_field( $slug ); | |
| 484 | + } | |
| 485 | + | |
| 486 | + /** | |
| 487 | + * Find the `slug` attribute of the first block with the given name. | |
| 488 | + * | |
| 489 | + * The inverse of find_block_name_by_slug(). Walks in document order, parents | |
| 490 | + * before children, so "first match" is stable and matches what the editor | |
| 491 | + * shows the author. | |
| 492 | + * | |
| 493 | + * @since 1.6.0 | |
| 494 | + * @param array<mixed> $blocks Array of parsed blocks. | |
| 495 | + * @param string $block_name Block name to look for. | |
| 496 | + * @return string|null The slug, or null when the block is absent or has no slug. | |
| 497 | + */ | |
| 498 | + private static function find_slug_by_block_name( $blocks, $block_name ) { | |
| 499 | + if ( ! is_array( $blocks ) ) { | |
| 500 | + return null; | |
| 501 | + } | |
| 502 | + | |
| 503 | + foreach ( $blocks as $block ) { | |
| 504 | + if ( ! is_array( $block ) ) { | |
| 505 | + continue; | |
| 506 | + } | |
| 507 | + | |
| 508 | + if ( isset( $block['blockName'] ) && $block_name === $block['blockName'] | |
| 509 | + && isset( $block['attrs']['slug'] ) && is_string( $block['attrs']['slug'] ) | |
| 510 | + && '' !== $block['attrs']['slug'] ) { | |
| 511 | + return $block['attrs']['slug']; | |
| 512 | + } | |
| 513 | + | |
| 514 | + if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) { | |
| 515 | + $found = self::find_slug_by_block_name( $block['innerBlocks'], $block_name ); | |
| 516 | + if ( null !== $found ) { | |
| 517 | + return $found; | |
| 339 | 518 | } |
| 340 | 519 | } |
| 341 | 520 | } |
| 342 | 521 | |
| 343 | - return array_values( array_unique( $slugs ) ); | |
| 522 | + return null; | |
| 344 | 523 | } |
| 345 | 524 | |
| 346 | 525 | /** |
| 347 | 526 | * Find the suredonation/payment block's attributes recursively. |
| @@ -375,8 +554,80 @@ | ||
| 375 | 554 | return null; |
| 376 | 555 | } |
| 377 | 556 | |
| 378 | 557 | /** |
| 558 | + * Derive the billing cadence from a payment block's attributes. | |
| 559 | + * | |
| 560 | + * Single source of truth shared by build_amount_config() (store time) and | |
| 561 | + * resolve_subscription_cadence_from_content() (legacy re-resolve). block.json's | |
| 562 | + * subscriptionPlan default is a fully-populated object, so Gutenberg omits the | |
| 563 | + * attribute whenever the admin accepts the defaults — hence the PHP-side | |
| 564 | + * defaults of month / ongoing here. | |
| 565 | + * | |
| 566 | + * @since 1.5.1 | |
| 567 | + * @param array<string, mixed> $attrs Parsed payment-block attributes. | |
| 568 | + * @return array{subscription_interval: string, subscription_billing_cycles: int|string} | |
| 569 | + */ | |
| 570 | + private static function derive_subscription_cadence_from_attrs( $attrs ) { | |
| 571 | + $plan = isset( $attrs['subscriptionPlan'] ) && is_array( $attrs['subscriptionPlan'] ) | |
| 572 | + ? $attrs['subscriptionPlan'] | |
| 573 | + : []; | |
| 574 | + | |
| 575 | + $interval = isset( $plan['interval'] ) && is_string( $plan['interval'] ) | |
| 576 | + ? sanitize_text_field( $plan['interval'] ) | |
| 577 | + : 'month'; | |
| 578 | + | |
| 579 | + if ( isset( $plan['billingCycles'] ) ) { | |
| 580 | + // billingCycles is either an integer count or the string 'ongoing'. | |
| 581 | + $cycles = $plan['billingCycles']; | |
| 582 | + $billing_cycles = is_numeric( $cycles ) | |
| 583 | + ? (int) $cycles | |
| 584 | + : sanitize_text_field( Helper::get_string_value( $cycles ) ); | |
| 585 | + } else { | |
| 586 | + $billing_cycles = 'ongoing'; | |
| 587 | + } | |
| 588 | + | |
| 589 | + return [ | |
| 590 | + 'subscription_interval' => $interval, | |
| 591 | + 'subscription_billing_cycles' => $billing_cycles, | |
| 592 | + ]; | |
| 593 | + } | |
| 594 | + | |
| 595 | + /** | |
| 596 | + * Re-resolve a form's billing cadence from its stored post content. | |
| 597 | + * | |
| 598 | + * Forms saved before the cadence was persisted into the block config carry no | |
| 599 | + * subscription_interval/billing_cycles keys in stored meta, and the config is | |
| 600 | + * only rebuilt on save_post — so those keys never appear until the admin | |
| 601 | + * happens to re-save. Rather than let the submit path assume month / ongoing | |
| 602 | + * (which silently rewrites the admin's real plan — e.g. a 5-cycle yearly plan | |
| 603 | + * becomes ongoing monthly), re-derive from the parsed payment block. This is | |
| 604 | + * still server-side and untamperable: it reads post_content, never the request. | |
| 605 | + * | |
| 606 | + * @since 1.5.1 | |
| 607 | + * @param int $form_id Donation form post ID. | |
| 608 | + * @return array{subscription_interval: string, subscription_billing_cycles: int|string}|null | |
| 609 | + * Cadence, or null when the form has no readable payment block. | |
| 610 | + */ | |
| 611 | + public static function resolve_subscription_cadence_from_content( $form_id ) { | |
| 612 | + if ( ! is_int( $form_id ) || $form_id <= 0 || ! function_exists( 'parse_blocks' ) ) { | |
| 613 | + return null; | |
| 614 | + } | |
| 615 | + | |
| 616 | + $post = get_post( $form_id ); | |
| 617 | + if ( ! ( $post instanceof \WP_Post ) || empty( $post->post_content ) ) { | |
| 618 | + return null; | |
| 619 | + } | |
| 620 | + | |
| 621 | + $attrs = self::find_payment_block_attrs( parse_blocks( $post->post_content ) ); | |
| 622 | + if ( ! is_array( $attrs ) ) { | |
| 623 | + return null; | |
| 624 | + } | |
| 625 | + | |
| 626 | + return self::derive_subscription_cadence_from_attrs( $attrs ); | |
| 627 | + } | |
| 628 | + | |
| 629 | + /** | |
| 379 | 630 | * Resolve the slug of the field mapped to the donor phone on the payment block. |
| 380 | 631 | * |
| 381 | 632 | * The mapping is optional: when an author maps a Phone field via the payment |
| 382 | 633 | * block's "Customer Phone Field" picker, its value is stored in the dedicated |
| @@ -742,8 +993,103 @@ | ||
| 742 | 993 | return $url_config; |
| 743 | 994 | } |
| 744 | 995 | |
| 745 | 996 | /** |
| 997 | + * Process donor comment block configuration. | |
| 998 | + * | |
| 999 | + * Stores required state, max length and the optional per-field custom | |
| 1000 | + * required message for server-side enforcement. Mirrors the text input's | |
| 1001 | + * rules — the field is a plain textarea with no format constraint. | |
| 1002 | + * | |
| 1003 | + * @param array<mixed> $attrs Block attributes. | |
| 1004 | + * @return array<string, mixed> Processed donor comment block configuration. | |
| 1005 | + * @since 1.6.0 | |
| 1006 | + */ | |
| 1007 | + private static function process_donor_comment_block( $attrs ) { | |
| 1008 | + $comment_config = [ | |
| 1009 | + 'required' => ! empty( $attrs['required'] ), | |
| 1010 | + 'max_length' => isset( $attrs['maxLength'] ) ? absint( Helper::get_string_value( $attrs['maxLength'] ) ) : 500, | |
| 1011 | + ]; | |
| 1012 | + | |
| 1013 | + $error_msg = isset( $attrs['errorMsg'] ) ? sanitize_text_field( Helper::get_string_value( $attrs['errorMsg'] ) ) : ''; | |
| 1014 | + if ( '' !== $error_msg ) { | |
| 1015 | + $comment_config['error_msg'] = $error_msg; | |
| 1016 | + } | |
| 1017 | + | |
| 1018 | + return $comment_config; | |
| 1019 | + } | |
| 1020 | + | |
| 1021 | + /** | |
| 1022 | + * Process checkbox block configuration. | |
| 1023 | + * | |
| 1024 | + * A checkbox carries no format or range rules — only the required flag and | |
| 1025 | + * the optional per-field error message. `is_checkbox` is stored so the | |
| 1026 | + * submission handler can recognise the field by its saved configuration | |
| 1027 | + * (rather than trusting the request) when it renders the value as Yes/No. | |
| 1028 | + * | |
| 1029 | + * @param array<mixed> $attrs Block attributes. | |
| 1030 | + * @return array<string, mixed> Processed checkbox configuration. | |
| 1031 | + * @since 1.5.1 | |
| 1032 | + */ | |
| 1033 | + private static function process_checkbox_block( $attrs ) { | |
| 1034 | + $checkbox_config = [ | |
| 1035 | + 'required' => ! empty( $attrs['required'] ), | |
| 1036 | + 'is_checkbox' => true, | |
| 1037 | + ]; | |
| 1038 | + | |
| 1039 | + $error_msg = isset( $attrs['errorMsg'] ) ? sanitize_text_field( Helper::get_string_value( $attrs['errorMsg'] ) ) : ''; | |
| 1040 | + if ( '' !== $error_msg ) { | |
| 1041 | + $checkbox_config['error_msg'] = $error_msg; | |
| 1042 | + } | |
| 1043 | + | |
| 1044 | + return $checkbox_config; | |
| 1045 | + } | |
| 1046 | + | |
| 1047 | + /** | |
| 1048 | + * Resolve the slugs of the form's checkbox fields. | |
| 1049 | + * | |
| 1050 | + * Read from the saved form's stored block configuration, so a submission | |
| 1051 | + * cannot claim a field is (or is not) a checkbox. Used to render the | |
| 1052 | + * submitted value as a readable Yes/No rather than a bare "1"/empty, and to | |
| 1053 | + * keep an unchecked box in the stored record instead of dropping it as an | |
| 1054 | + * empty value. | |
| 1055 | + * | |
| 1056 | + * Only `suredonation/checkbox` fields are returned. The fixed-purpose | |
| 1057 | + * consent checkboxes (anonymous donation, cover fees, privacy consent) are | |
| 1058 | + * not form-editor field blocks and have no entry in the block config, so | |
| 1059 | + * their storage is unaffected. | |
| 1060 | + * | |
| 1061 | + * @param int $form_id Donation form post ID. | |
| 1062 | + * @return array<int, string> Checkbox field slugs. | |
| 1063 | + * @since 1.5.1 | |
| 1064 | + */ | |
| 1065 | + public static function get_checkbox_field_slugs( $form_id ) { | |
| 1066 | + $form_id = absint( $form_id ); | |
| 1067 | + if ( $form_id <= 0 ) { | |
| 1068 | + return []; | |
| 1069 | + } | |
| 1070 | + | |
| 1071 | + $block_config = self::get_or_migrate_block_config_for_legacy_form( $form_id ); | |
| 1072 | + if ( empty( $block_config ) || ! is_array( $block_config ) ) { | |
| 1073 | + return []; | |
| 1074 | + } | |
| 1075 | + | |
| 1076 | + $slugs = []; | |
| 1077 | + foreach ( $block_config as $config ) { | |
| 1078 | + if ( ! is_array( $config ) || empty( $config['is_checkbox'] ) ) { | |
| 1079 | + continue; | |
| 1080 | + } | |
| 1081 | + | |
| 1082 | + $slug = isset( $config['slug'] ) && is_string( $config['slug'] ) ? $config['slug'] : ''; | |
| 1083 | + if ( '' !== $slug ) { | |
| 1084 | + $slugs[] = $slug; | |
| 1085 | + } | |
| 1086 | + } | |
| 1087 | + | |
| 1088 | + return $slugs; | |
| 1089 | + } | |
| 1090 | + | |
| 1091 | + /** | |
| 746 | 1092 | * Get the block types that participate in field validation. |
| 747 | 1093 | * |
| 748 | 1094 | * Extensions register new validatable field blocks (e.g. phone, address, |
| 749 | 1095 | * url) via the filter so their values run through validate_form_data(). |
| @@ -869,11 +1215,13 @@ | ||
| 869 | 1215 | $messages = [ |
| 870 | 1216 | 'suredonation_input_block_required_text' => __( 'This field is required.', 'suredonation' ), |
| 871 | 1217 | 'suredonation_email_block_required_text' => __( 'This field is required.', 'suredonation' ), |
| 872 | 1218 | 'suredonation_number_block_required_text' => __( 'This field is required.', 'suredonation' ), |
| 1219 | + 'suredonation_checkbox_block_required_text' => __( 'This field is required.', 'suredonation' ), | |
| 873 | 1220 | 'suredonation_dropdown_block_required_text' => __( 'This field is required.', 'suredonation' ), |
| 874 | 1221 | 'suredonation_phone_block_required_text' => __( 'This field is required.', 'suredonation' ), |
| 875 | 1222 | 'suredonation_url_block_required_text' => __( 'This field is required.', 'suredonation' ), |
| 1223 | + 'suredonation_donor_comment_block_required_text' => __( 'This field is required.', 'suredonation' ), | |
| 876 | 1224 | 'suredonation_valid_email' => __( 'Please enter a valid email address.', 'suredonation' ), |
| 877 | 1225 | 'suredonation_valid_number' => __( 'Please enter a valid number.', 'suredonation' ), |
| 878 | 1226 | 'suredonation_valid_phone' => __( 'Please enter a valid phone number.', 'suredonation' ), |
| 879 | 1227 | 'suredonation_valid_url' => __( 'Please enter a valid URL.', 'suredonation' ), |
| @@ -957,8 +1305,9 @@ | ||
| 957 | 1305 | } |
| 958 | 1306 | |
| 959 | 1307 | switch ( $block_name ) { |
| 960 | 1308 | case 'suredonation/input': |
| 1309 | + case 'suredonation/donor-comment': | |
| 961 | 1310 | $max_length = isset( $config['max_length'] ) && is_numeric( $config['max_length'] ) ? (int) $config['max_length'] : 0; |
| 962 | 1311 | $length = function_exists( 'mb_strlen' ) ? mb_strlen( $value ) : strlen( $value ); |
| 963 | 1312 | if ( $max_length > 0 && $length > $max_length ) { |
| 964 | 1313 | // str_replace (not sprintf) because the message is admin/translator |