| @@ -22,8 +22,45 @@ | ||
| 22 | 22 | return 2; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | + * Resolve an actionable visitor message for a Booking Form context failure. | |
| 27 | + * | |
| 28 | + * The frontend message registry makes these notices translatable and editable | |
| 29 | + * with the other Form Messages. The local defaults keep this security boundary | |
| 30 | + * usable in isolated tests and integrations that load the context API before | |
| 31 | + * the central message registry. | |
| 32 | + * | |
| 33 | + * @param string $message_key Stable frontend message key. | |
| 34 | + * @param mixed $resource_id Booking Resource ID associated with the form. | |
| 35 | + * | |
| 36 | + * @return string Plain-text visitor message, or an empty string for an unsupported key. | |
| 37 | + */ | |
| 38 | +function wpbc_classic_booking_context_get_visitor_message( $message_key, $resource_id = 0 ) { | |
| 39 | + $resource_id = absint( $resource_id ); | |
| 40 | + $fallbacks = array( | |
| 41 | + /* translators: Keep the {resource_id} placeholder unchanged. */ | |
| 42 | + 'message_booking_form_context_required' => __( 'This booking form is not connected to a valid calendar for Booking Resource ID {resource_id}. This can happen when the same Booking Resource is used more than once on the page, including in hidden content, or when the page cache is outdated. Reload the page and try again. If the problem continues, ask the site administrator to remove duplicate forms for this Booking Resource and clear the page cache.', 'booking' ), | |
| 43 | + /* translators: Keep the {resource_id} placeholder unchanged. */ | |
| 44 | + 'message_booking_form_context_expired' => __( 'This booking form was generated by an older or cached version of the page for Booking Resource ID {resource_id}. Reload the page and try again. If the problem continues, ask the site administrator to clear the page cache and remove any duplicate calendar or form for this Booking Resource.', 'booking' ), | |
| 45 | + ); | |
| 46 | + | |
| 47 | + if ( ! isset( $fallbacks[ $message_key ] ) ) { | |
| 48 | + return ''; | |
| 49 | + } | |
| 50 | + | |
| 51 | + $replacements = array( '{resource_id}' => (string) $resource_id ); | |
| 52 | + if ( function_exists( 'wpbc_frontend_messages__get' ) ) { | |
| 53 | + $resolved_message = wpbc_frontend_messages__get( $message_key, $replacements, $resource_id ); | |
| 54 | + if ( '' !== $resolved_message ) { | |
| 55 | + return $resolved_message; | |
| 56 | + } | |
| 57 | + } | |
| 58 | + | |
| 59 | + return strtr( $fallbacks[ $message_key ], $replacements ); | |
| 60 | +} | |
| 61 | + | |
| 62 | +/** | |
| 26 | 63 | * Normalize one YYYY-MM-DD value and reject impossible calendar dates. |
| 27 | 64 | * |
| 28 | 65 | * @param mixed $date_value Candidate date value. |
| 29 | 66 | * |
| @@ -253,9 +290,12 @@ | ||
| 253 | 290 | } |
| 254 | 291 | |
| 255 | 292 | $context = wpbc_classic_booking_context_normalize( $context ); |
| 256 | 293 | if ( wpbc_classic_booking_context_get_version() !== $context['context_version'] ) { |
| 257 | - return new WP_Error( 'classic_booking_context_expired', __( 'The booking form context has expired. Please reload the page and try again.', 'booking' ) ); | |
| 294 | + return new WP_Error( | |
| 295 | + 'classic_booking_context_expired', | |
| 296 | + wpbc_classic_booking_context_get_visitor_message( 'message_booking_form_context_expired', $context['resource_id'] ) | |
| 297 | + ); | |
| 258 | 298 | } |
| 259 | 299 | if ( |
| 260 | 300 | 0 === $context['resource_id'] |
| 261 | 301 | || ( ( '' === $context['calendar_dates_start'] ) !== ( '' === $context['calendar_dates_end'] ) ) |