carrying data-* attributes; assets/js/expressmaps-frontend.js * reads them and initialises a MapLibre map. All values are sanitised here * and escaped on output. * * @package ExpressMaps */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Default attribute set shared by the shortcode and the block. * * @return array */ function expressmaps_default_atts() { return array( 'lat' => 51.5, 'lng' => -0.12, 'zoom' => 13, 'style' => '', 'height' => '400px', 'width' => '100%', 'marker' => true, 'popup' => '', 'controls' => true, 'key' => '', ); } /** * Normalise raw attributes into a safe, typed array. * * @param array $atts Raw attributes (shortcode or block). * @return array */ function expressmaps_normalize_atts( $atts ) { $atts = wp_parse_args( (array) $atts, expressmaps_default_atts() ); $key = '' !== (string) $atts['key'] ? expressmaps_sanitize_api_key( $atts['key'] ) : expressmaps_get_api_key(); return array( 'lat' => expressmaps_clamp( $atts['lat'], -90, 90, 51.5 ), 'lng' => expressmaps_clamp( $atts['lng'], -180, 180, -0.12 ), 'zoom' => expressmaps_clamp( $atts['zoom'], 0, 22, 13 ), 'style' => expressmaps_sanitize_style( $atts['style'], expressmaps_get_default_style() ), 'height' => expressmaps_sanitize_length( $atts['height'] ) ?: '400px', 'width' => expressmaps_sanitize_length( $atts['width'] ) ?: '100%', 'marker' => expressmaps_to_bool( $atts['marker'], true ), 'popup' => sanitize_text_field( (string) $atts['popup'] ), 'controls' => expressmaps_to_bool( $atts['controls'], true ), 'key' => $key, ); } /** * Enqueue MapLibre and the front-end script. Called only when a map is rendered, * so the assets are loaded solely on pages that contain a shortcode or block. */ function expressmaps_enqueue_frontend() { if ( ! wp_script_is( 'expressmaps-frontend', 'registered' ) ) { expressmaps_register_assets(); } wp_enqueue_style( 'expressmaps-frontend' ); wp_enqueue_script( 'expressmaps-frontend' ); } /** * Render a map container. * * @param array $atts Raw attributes. * @param string $extra_classes Extra CSS classes (e.g. block wrapper classes). * @return string HTML */ function expressmaps_render_map( $atts, $extra_classes = '' ) { $a = expressmaps_normalize_atts( $atts ); if ( '' === $a['key'] ) { $msg = __( 'ExpressMaps: no API key set. Add one under Settings › ExpressMaps.', 'expressmaps' ); if ( current_user_can( 'manage_options' ) ) { return '