| 1 |
<?php |
| 2 |
/** |
| 3 |
* Google Map block server-side render. |
| 4 |
* |
| 5 |
* Emits an empty wrapper carrying the block attributes; view.js hydrates it |
| 6 |
* with React (Components/Common/Style.js + Components/Frontend/GoogleMap.js). |
| 7 |
* The API key is injected here as a data attribute so it never lives in post |
| 8 |
* content, and a static address + deep link is printed inside the wrapper so |
| 9 |
* no-JS / no-key visitors still get something useful before hydration wipes it. |
| 10 |
* |
| 11 |
* @package bBlocks |
| 12 |
* |
| 13 |
* @var array $attributes Block attributes. |
| 14 |
*/ |
| 15 |
|
| 16 |
$prefix = 'bBlocksGoogleMap'; |
| 17 |
$id = wp_unique_id( "$prefix-" ); |
| 18 |
$api_keys = get_option( 'bBlocksApiKeys', [] ); |
| 19 |
$planClass = BBlocks\Inc\Utils::isPro() ? 'pro' : 'free'; |
| 20 |
|
| 21 |
$source = isset( $attributes['source'] ) && is_array( $attributes['source'] ) ? $attributes['source'] : []; |
| 22 |
$markers = isset( $attributes['markers'] ) && is_array( $attributes['markers'] ) ? $attributes['markers'] : []; |
| 23 |
$caption = isset( $attributes['caption'] ) && is_array( $attributes['caption'] ) ? $attributes['caption'] : []; |
| 24 |
|
| 25 |
// Best available human-readable location for the static fallback. |
| 26 |
$fallback_address = ''; |
| 27 |
if ( ! empty( $source['center']['address'] ) ) { |
| 28 |
$fallback_address = $source['center']['address']; |
| 29 |
} elseif ( ! empty( $markers[0]['address'] ) ) { |
| 30 |
$fallback_address = $markers[0]['address']; |
| 31 |
} elseif ( ! empty( $markers[0]['title'] ) ) { |
| 32 |
$fallback_address = $markers[0]['title']; |
| 33 |
} elseif ( ! empty( $caption['text'] ) ) { |
| 34 |
$fallback_address = $caption['text']; |
| 35 |
} |
| 36 |
|
| 37 |
// Fall back to raw coordinates so the deep link always resolves somewhere. |
| 38 |
$fallback_query = $fallback_address; |
| 39 |
if ( '' === $fallback_query && isset( $source['center']['lat'], $source['center']['lng'] ) ) { |
| 40 |
$fallback_query = $source['center']['lat'] . ',' . $source['center']['lng']; |
| 41 |
} |
| 42 |
|
| 43 |
$maps_url = $fallback_query ? 'https://www.google.com/maps/search/?api=1&query=' . rawurlencode( $fallback_query ) : 'https://www.google.com/maps'; |
| 44 |
?> |
| 45 |
<div |
| 46 |
<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() is properly escaped ?> |
| 47 |
<?php echo get_block_wrapper_attributes( [ 'class' => $planClass ] ); ?> |
| 48 |
id='<?php echo esc_attr( $id ); ?>' |
| 49 |
data-attributes='<?php echo esc_attr( wp_json_encode( $attributes ) ); ?>' |
| 50 |
data-mapkey='<?php echo esc_attr( $api_keys['googleMaps']['key'] ?? '' ); ?>' |
| 51 |
> |
| 52 |
<div class='b-blocks-google-map-fallback'> |
| 53 |
<?php if ( $fallback_address ) : ?> |
| 54 |
<p class='b-blocks-google-map-fallback-address'><?php echo esc_html( $fallback_address ); ?></p> |
| 55 |
<?php endif; ?> |
| 56 |
|
| 57 |
<a class='b-blocks-google-map-fallback-link' href='<?php echo esc_url( $maps_url ); ?>' target='_blank' rel='noopener noreferrer'> |
| 58 |
<?php esc_html_e( 'Open in Google Maps', 'b-blocks' ); ?> |
| 59 |
</a> |
| 60 |
</div> |
| 61 |
</div> |
| 62 |
|