slug => label */ function expressmaps_get_styles() { return array( 'standard' => __( 'Standard', 'expressmaps' ), 'basic' => __( 'Basic', 'expressmaps' ), 'enhanced' => __( 'Enhanced', 'expressmaps' ), 'darklands' => __( 'Darklands', 'expressmaps' ), 'greyscale' => __( 'Greyscale', 'expressmaps' ), 'navigator' => __( 'Navigator', 'expressmaps' ), 'subtle' => __( 'Subtle', 'expressmaps' ), 'autobahn' => __( 'Autobahn', 'expressmaps' ), 'lithium' => __( 'Lithium', 'expressmaps' ), 'mulberry' => __( 'Mulberry', 'expressmaps' ), ); } /** * Sanitise a style slug, falling back to the given default. * * @param mixed $style Raw style value. * @param string $default Fallback slug. * @return string */ function expressmaps_sanitize_style( $style, $default = 'standard' ) { $style = strtolower( sanitize_key( (string) $style ) ); if ( array_key_exists( $style, expressmaps_get_styles() ) ) { return $style; } return array_key_exists( $default, expressmaps_get_styles() ) ? $default : 'standard'; } /** * Sanitise an API key: printable, no whitespace, limited length. * * @param mixed $key Raw key. * @return string */ function expressmaps_sanitize_api_key( $key ) { $key = trim( sanitize_text_field( (string) $key ) ); $key = preg_replace( '/[^A-Za-z0-9_\-\.]/', '', $key ); return substr( (string) $key, 0, 128 ); } /** * Stored API key. * * @return string */ function expressmaps_get_api_key() { return expressmaps_sanitize_api_key( get_option( 'expressmaps_api_key', '' ) ); } /** * Stored default style. * * @return string */ function expressmaps_get_default_style() { return expressmaps_sanitize_style( get_option( 'expressmaps_default_style', 'standard' ) ); } /** * Build the style URL for a style and key. * * @param string $style Style slug (already sanitised). * @param string $key API key (already sanitised). * @return string */ function expressmaps_style_url( $style, $key ) { return EXPRESSMAPS_STYLE_BASE . rawurlencode( $style ) . '.json?key=' . rawurlencode( $key ); } /** * Interpret a loose boolean ("true", "1", "yes", "on"). * * @param mixed $value Raw value. * @param bool $default Fallback if not recognisable. * @return bool */ function expressmaps_to_bool( $value, $default = false ) { if ( is_bool( $value ) ) { return $value; } $value = strtolower( trim( (string) $value ) ); if ( '' === $value ) { return $default; } return in_array( $value, array( '1', 'true', 'yes', 'on' ), true ); } /** * Sanitise a CSS length (e.g. "400px", "50vh", "100%"). Returns '' if invalid. * * @param mixed $value Raw length. * @return string */ function expressmaps_sanitize_length( $value ) { $value = trim( (string) $value ); if ( preg_match( '/^\d+(\.\d+)?(px|%|em|rem|vh|vw)?$/', $value, $m ) ) { return empty( $m[2] ) ? $value . 'px' : $value; } return ''; } /** * Clamp a float to a range; returns $default if not numeric. * * @param mixed $value Raw value. * @param float $min Minimum. * @param float $max Maximum. * @param float $default Fallback. * @return float */ function expressmaps_clamp( $value, $min, $max, $default ) { if ( ! is_numeric( $value ) ) { return (float) $default; } return (float) max( $min, min( $max, (float) $value ) ); }