| 1 |
<?php |
| 2 |
namespace WPMapBlock; |
| 3 |
|
| 4 |
class Block |
| 5 |
{ |
| 6 |
public static function init() |
| 7 |
{ |
| 8 |
$self = new self(); |
| 9 |
add_action('init', [$self, 'register_block']); |
| 10 |
} |
| 11 |
|
| 12 |
public function register_block() |
| 13 |
{ |
| 14 |
register_block_type( |
| 15 |
'wpmapblock/wp-map-block', |
| 16 |
array( |
| 17 |
'editor_style_handles' => [ 'wp-map-block-editor-css', 'wp-map-block-stylesheets'], |
| 18 |
'editor_script_handles' => [ 'wp-map-block-js', 'wp-map-block-frontend-js'], |
| 19 |
'render_callback' => [$this, 'render_callback'], |
| 20 |
) |
| 21 |
); |
| 22 |
} |
| 23 |
public static function escaping_array_data($array) |
| 24 |
{ |
| 25 |
foreach ($array as $key => &$value) { |
| 26 |
if (is_array($value)) { |
| 27 |
$value = self::escaping_array_data($value); |
| 28 |
} else { |
| 29 |
$value = esc_attr(wp_kses_post($value)); |
| 30 |
} |
| 31 |
} |
| 32 |
return $array; |
| 33 |
} |
| 34 |
public function render_callback($attributes, $content = '') |
| 35 |
{ |
| 36 |
wp_enqueue_style('wp-map-block-stylesheets'); |
| 37 |
wp_enqueue_script('wpmapblock-leaflet'); |
| 38 |
wp_enqueue_script('wpmapblock-leaflet-fullscreen'); |
| 39 |
wp_enqueue_script('wp-map-block-frontend-js'); |
| 40 |
|
| 41 |
$settings = [ |
| 42 |
'map_marker' => $this->escaping_array_data(isset($attributes['map_marker_list']) ? $attributes['map_marker_list'] : []), |
| 43 |
'map_zoom' => (isset($attributes['map_zoom']) ? esc_attr($attributes['map_zoom']) : 10), |
| 44 |
'scroll_wheel_zoom' => (isset($attributes['scroll_wheel_zoom']) ? esc_attr($attributes['scroll_wheel_zoom']) : false), |
| 45 |
'map_type' => (isset($attributes['map_type']) ? esc_attr($attributes['map_type']) : 'GM'), |
| 46 |
'center_index' => (isset($attributes['center_index']) ? intval(esc_attr($attributes['center_index'])) : 0), |
| 47 |
] ; |
| 48 |
|
| 49 |
$map_width = (isset($attributes['map_width']) ? esc_attr($attributes['map_width']) . '%' : '100%'); |
| 50 |
$map_height = (isset($attributes['map_height']) ? esc_attr($attributes['map_height']) . 'px' : '500px'); |
| 51 |
$style = " |
| 52 |
width: {$map_width}; |
| 53 |
height: {$map_height}; |
| 54 |
"; |
| 55 |
|
| 56 |
ob_start(); ?> |
| 57 |
<div id="<?php echo(isset($attributes['map_id']) ? esc_attr($attributes['map_id']) : ''); ?>" data-settings='<?php echo htmlspecialchars(json_encode($settings), ENT_QUOTES, 'UTF-8'); ?>' class="wpmapblockrender" style="<?php echo esc_attr($style); ?>"></div> |
| 58 |
<?php |
| 59 |
$output = ob_get_clean(); |
| 60 |
return $output; |
| 61 |
} |
| 62 |
} |
| 63 |
|