array( 'type' => 'number', 'default' => 51.5, ), 'lng' => array( 'type' => 'number', 'default' => -0.12, ), 'zoom' => array( 'type' => 'number', 'default' => 13, ), 'style' => array( 'type' => 'string', 'default' => '', ), 'height' => array( 'type' => 'string', 'default' => '400px', ), 'width' => array( 'type' => 'string', 'default' => '100%', ), 'marker' => array( 'type' => 'boolean', 'default' => true, ), 'popup' => array( 'type' => 'string', 'default' => '', ), 'controls' => array( 'type' => 'boolean', 'default' => true, ), 'key' => array( 'type' => 'string', 'default' => '', ), ); } /** * Register the block, its editor script and the shared editor data. */ function expressmaps_register_block() { if ( ! function_exists( 'register_block_type' ) ) { return; } wp_register_script( 'expressmaps-block-editor', EXPRESSMAPS_URL . 'assets/js/expressmaps-block.js', array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-i18n', 'expressmaps-frontend' ), EXPRESSMAPS_VERSION, true ); // Data the editor needs to build a live preview. The key is only sent to // users who can edit posts; it is the same key already exposed to visitors // in the style URL on the front end. $styles = array(); foreach ( expressmaps_get_styles() as $slug => $label ) { $styles[] = array( 'value' => $slug, 'label' => $label, ); } wp_add_inline_script( 'expressmaps-block-editor', 'window.expressmapsBlockData = ' . wp_json_encode( array( 'styleBase' => EXPRESSMAPS_STYLE_BASE, 'styles' => $styles, 'defaultStyle' => expressmaps_get_default_style(), 'hasKey' => '' !== expressmaps_get_api_key(), 'apiKey' => expressmaps_get_api_key(), 'settingsUrl' => admin_url( 'options-general.php?page=expressmaps' ), 'maplibreJs' => EXPRESSMAPS_MAPLIBRE_JS, 'maplibreCss' => EXPRESSMAPS_MAPLIBRE_CSS, 'pluginCss' => EXPRESSMAPS_URL . 'assets/css/expressmaps.css', ) ) . ';', 'before' ); register_block_type( 'expressmaps/map', array( 'api_version' => 2, 'title' => __( 'ExpressMaps Map', 'expressmaps' ), 'description' => __( 'Embed a fast OpenStreetMap-based vector map from ExpressMaps.', 'expressmaps' ), 'category' => 'embed', 'icon' => 'location-alt', 'keywords' => array( 'map', 'openstreetmap', 'expressmaps', 'location' ), 'textdomain' => 'expressmaps', 'supports' => array( 'html' => false, 'align' => array( 'wide', 'full' ), ), 'attributes' => expressmaps_block_attributes(), 'editor_script' => 'expressmaps-block-editor', 'editor_style' => 'expressmaps-frontend', 'render_callback' => 'expressmaps_render_block', ) ); } add_action( 'init', 'expressmaps_register_block' ); /** * Server-side render for the block: delegates to the shared renderer so the * front-end markup is identical to the shortcode. * * @param array $attributes Block attributes. * @return string */ function expressmaps_render_block( $attributes ) { $classes = ''; if ( ! empty( $attributes['align'] ) && in_array( $attributes['align'], array( 'wide', 'full' ), true ) ) { $classes = 'align' . $attributes['align']; } return expressmaps_render_map( $attributes, $classes ); }