` prefix Wavind puts in front of a path; English has none. */ function wavind_locale_prefix( $locale ) { return 'en' === $locale ? '' : '/' . $locale; } /** * The spot's `slug--id` from whatever the user gave: the param itself, a bare * id, or a Wavind forecast / embed URL copied from the browser. * * @return string Empty when nothing usable was found. */ function wavind_parse_spot_param( $input ) { $input = trim( (string) $input ); if ( '' === $input ) { return ''; } if ( preg_match( '~/spots/([^/?#]+)~i', $input, $matches ) ) { $input = rawurldecode( $matches[1] ); } $input = strtolower( $input ); $uuid = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'; return preg_match( '~^(?:[a-z0-9-]+--)?' . $uuid . '$~', $input ) ? $input : ''; } /** * The spot name read back out of the slug, for a shortcode written without * `name`. The block always stores the real name from the search. */ function wavind_spot_name_from_param( $spot_param ) { $separator = strrpos( $spot_param, '--' ); if ( false === $separator ) { return 'Wavind'; } $words = array_filter( explode( '-', substr( $spot_param, 0, $separator ) ) ); return $words ? implode( ' ', array_map( 'ucfirst', $words ) ) : 'Wavind'; } /** * Normalise forecast options. Unknown values fall back to the defaults, as the * widget itself does: a typo in a shortcode should not blank the widget. */ function wavind_forecast_args( array $args ) { $days = isset( $args['days'] ) ? (int) $args['days'] : 5; return array( 'spot' => wavind_parse_spot_param( isset( $args['spot'] ) ? $args['spot'] : '' ), 'name' => isset( $args['name'] ) ? sanitize_text_field( (string) $args['name'] ) : '', 'days' => max( 1, min( 7, $days ) ), 'theme' => wavind_pick( isset( $args['theme'] ) ? $args['theme'] : '', array( 'auto', 'light', 'dark' ), 'auto' ), 'units' => wavind_pick( isset( $args['units'] ) ? $args['units'] : '', array( 'kts', 'ms', 'kmh' ), 'kts' ), 'temp' => wavind_pick( isset( $args['temp'] ) ? $args['temp'] : '', array( 'c', 'f' ), 'c' ), 'waves' => wavind_pick( isset( $args['waves'] ) ? $args['waves'] : '', array( 'auto', 'm', 'ft', 'off' ), 'auto' ), 'sport' => wavind_pick( isset( $args['sport'] ) ? $args['sport'] : '', array( 'wind', 'surf', 'optimal' ), 'wind' ), 'locale' => wavind_pick( isset( $args['locale'] ) && '' !== $args['locale'] ? $args['locale'] : wavind_default_locale(), WAVIND_LOCALES, 'en' ), 'credit' => isset( $args['credit'] ) ? wavind_bool( $args['credit'] ) : false, ); } /** The widget URL. Defaults are left out, exactly as the web app writes it. */ function wavind_forecast_src( array $args ) { $defaults = array( 'days' => 5, 'theme' => 'auto', 'units' => 'kts', 'temp' => 'c', 'waves' => 'auto', 'sport' => 'wind', 'locale' => 'en', ); $query = array(); foreach ( $defaults as $key => $default ) { if ( $args[ $key ] !== $default ) { $query[ $key ] = $args[ $key ]; } } $src = wavind_web_origin() . '/embed/spots/' . rawurlencode( $args['spot'] ); return $query ? $src . '?' . http_build_query( $query ) : $src; } /** The full forecast on Wavind, which the credit line links to. */ function wavind_forecast_page_url( array $args ) { return wavind_web_origin() . wavind_locale_prefix( $args['locale'] ) . '/forecasts/spots/' . rawurlencode( $args['spot'] ) . '?utm_source=embed&utm_medium=widget&utm_campaign=wordpress-plugin'; } /** * The credit line's text: spot name plus brand, never a keyword phrase, in one * of three wordings chosen by the spot so two sites on the same coast do not * print identical links. Same templates and hash as the web snippet. */ function wavind_attribution_text( $spot_name, $spot_param, $locale ) { $templates = array( 'en' => array( 'Wind forecast for %s — Wavind', '%s wind forecast on Wavind', '%s — live wind forecast by Wavind' ), 'pl' => array( 'Prognoza wiatru dla %s — Wavind', '%s — prognoza wiatru w Wavind', '%s — prognoza wiatru na żywo od Wavind' ), 'de' => array( 'Windvorhersage für %s — Wavind', '%s Windvorhersage auf Wavind', '%s — Live-Windvorhersage von Wavind' ), 'es' => array( 'Previsión de viento para %s — Wavind', 'Previsión de viento de %s en Wavind', '%s — previsión de viento en directo de Wavind' ), 'pt' => array( 'Previsão de vento para %s — Wavind', 'Previsão de vento de %s na Wavind', '%s — previsão de vento em direto da Wavind' ), 'fr' => array( 'Prévisions de vent pour %s — Wavind', 'Prévisions de vent de %s sur Wavind', '%s — prévisions de vent en direct par Wavind' ), ); $hash = 0; $length = strlen( $spot_param ); for ( $i = 0; $i < $length; $i++ ) { $hash = ( $hash * 31 + ord( $spot_param[ $i ] ) ) % 997; } $set = isset( $templates[ $locale ] ) ? $templates[ $locale ] : $templates['en']; return sprintf( $set[ $hash % 3 ], $spot_name ); } /** * Frame height that fits the table, reserving the wave rows unless they are * off, plus 12px for a visible scrollbar (`EMBED_SCROLLBAR_ALLOWANCE`). */ function wavind_forecast_height( array $args ) { return 'off' === $args['waves'] ? 282 : 318; } /** * The forecast widget: an iframe (no script runs on the site) and, only when * the site owner switched it on, a credit link under it. */ function wavind_forecast_html( array $raw_args ) { $args = wavind_forecast_args( $raw_args ); if ( '' === $args['spot'] ) { return current_user_can( 'edit_posts' ) ? '

' . esc_html__( 'Wavind forecast: choose a spot.', 'wavind' ) . '

' : ''; } $name = '' !== $args['name'] ? $args['name'] : wavind_spot_name_from_param( $args['spot'] ); $title = wavind_attribution_text( $name, $args['spot'], $args['locale'] ); $html = sprintf( '', esc_url( wavind_forecast_src( $args ) ), esc_attr( $title ), wavind_forecast_height( $args ) ); if ( $args['credit'] ) { $html .= sprintf( '

%2$s

', esc_url( wavind_forecast_page_url( $args ) ), esc_html( $title ) ); } return $html; } /** * The business slug from a booking link copied from Wavind * (`https://go.wavind.app/pt/book/surf-school?…`) or the slug itself. */ function wavind_parse_booking_slug( $input ) { $input = trim( (string) $input ); if ( preg_match( '~/book/([^/?#]+)~i', $input, $matches ) ) { $input = rawurldecode( $matches[1] ); } $input = strtolower( $input ); return preg_match( '~^[a-z0-9][a-z0-9-]{0,99}$~', $input ) ? $input : ''; } function wavind_booking_args( array $args ) { $link = isset( $args['link'] ) ? $args['link'] : ''; if ( '' === $link && isset( $args['business'] ) ) { $link = $args['business']; } // A pasted link already says which language the school picked on Wavind. $locale = isset( $args['locale'] ) ? $args['locale'] : ''; if ( '' === $locale && preg_match( '~^/(?:(en|pl|de|es|pt|fr)/)?book/[^/]+/?$~i', (string) wp_parse_url( $link, PHP_URL_PATH ), $matches ) ) { $locale = empty( $matches[1] ) ? 'en' : strtolower( $matches[1] ); } return array( 'slug' => wavind_parse_booking_slug( $link ), 'locale' => wavind_pick( '' !== $locale ? $locale : wavind_default_locale(), WAVIND_LOCALES, 'en' ), 'style' => wavind_pick( isset( $args['style'] ) ? $args['style'] : '', array( 'brand', 'dark', 'light' ), 'brand' ), 'popup' => isset( $args['popup'] ) ? wavind_bool( $args['popup'] ) : false, 'label' => isset( $args['label'] ) ? sanitize_text_field( (string) $args['label'] ) : '', ); } function wavind_booking_url( array $args ) { return wavind_web_origin() . wavind_locale_prefix( $args['locale'] ) . '/book/' . rawurlencode( $args['slug'] ) . '?utm_source=partner&utm_medium=button&utm_campaign=wordpress-plugin'; } /** The button's colours, from Wavind's light palette (@repo/design-tokens). */ function wavind_booking_css( $style ) { $colors = array( 'brand' => array( '#206b5f', '#ffffff', '#206b5f' ), 'dark' => array( '#161616', '#e8e8e8', '#161616' ), 'light' => array( '#e8e8e8', '#161616', '#cecece' ), ); list( $background, $color, $border ) = $colors[ $style ]; return "display:inline-block;padding:12px 24px;border:1px solid {$border};border-radius:14px;background:{$background};color:{$color};font-family:inherit;font-size:16px;font-weight:600;line-height:24px;text-decoration:none"; } /** * "Book on Wavind": always a link, never a frame — a Wavind page framed on the * school's site would not see the rider's sign-in. */ function wavind_booking_button_html( array $raw_args ) { $args = wavind_booking_args( $raw_args ); if ( '' === $args['slug'] ) { return current_user_can( 'edit_posts' ) ? '

' . esc_html__( 'Wavind booking button: paste your booking link from Wavind (My business → Share).', 'wavind' ) . '

' : ''; } $labels = array( 'en' => 'Book on Wavind', 'pl' => 'Zarezerwuj w Wavind', 'de' => 'Auf Wavind buchen', 'es' => 'Reserva en Wavind', 'pt' => 'Reservar no Wavind', 'fr' => 'Réserver sur Wavind', ); $label = '' !== $args['label'] ? $args['label'] : $labels[ $args['locale'] ]; $popup = $args['popup'] ? ' onclick="return !window.open(this.href,\'wavind\',\'popup,width=480,height=760\')"' : ''; return sprintf( '%4$s', esc_url( wavind_booking_url( $args ) ), $popup, esc_attr( wavind_booking_css( $args['style'] ) ), esc_html( $label ) ); }