| @@ -1,0 +1,136 @@ | ||
| 1 | +<?php | |
| 2 | +namespace ABlocks\Blocks\Map; | |
| 3 | + | |
| 4 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | + exit; | |
| 6 | +} | |
| 7 | + | |
| 8 | +use ABlocks\Classes\BlockBaseAbstract; | |
| 9 | +use ABlocks\Classes\CssGenerator; | |
| 10 | +use ABlocks\Classes\CssGeneratorV2; | |
| 11 | +use ABlocks\Controls\CssFilter; | |
| 12 | +use ABlocks\Controls\Range; | |
| 13 | + | |
| 14 | +class Block extends BlockBaseAbstract { | |
| 15 | + protected $block_name = 'map'; | |
| 16 | + protected $style_depends = [ 'ablocks-leaflet-style', 'ablocks-leaflet-full-screen-style' ]; | |
| 17 | + protected $script_depends = [ 'ablocks-leaflet-script', 'ablocks-leaflet-full-screen-script' ]; | |
| 18 | + | |
| 19 | + public function build_css_v1( $attributes ) { | |
| 20 | + $css_generator = new CssGenerator( $attributes, $this->block_name ); | |
| 21 | + | |
| 22 | + $css_generator->add_class_styles( | |
| 23 | + '{{WRAPPER}} .ablocks-map-block', | |
| 24 | + $this->get_map_size_css( $attributes ), | |
| 25 | + $this->get_map_size_css( $attributes, 'Tablet' ), | |
| 26 | + $this->get_map_size_css( $attributes, 'Mobile' ) | |
| 27 | + ); | |
| 28 | + | |
| 29 | + return $css_generator->generate_css(); | |
| 30 | + } | |
| 31 | + public function build_css_v2( $attributes ) { | |
| 32 | + $css_generator = new CssGeneratorV2( $attributes, $this->block_name ); | |
| 33 | + | |
| 34 | + $css_generator->add_class_styles( | |
| 35 | + '{{WRAPPER}} .ablocks-map-block', | |
| 36 | + $this->get_map_size_css( $attributes ), | |
| 37 | + $this->get_map_size_css( $attributes, 'Tablet' ), | |
| 38 | + $this->get_map_size_css( $attributes, 'Mobile' ) | |
| 39 | + ); | |
| 40 | + | |
| 41 | + return $css_generator->generate_css(); | |
| 42 | + } | |
| 43 | + public function build_css( $attributes ) { | |
| 44 | + if ( isset( $attributes['blockVersion'] ) && (int) $attributes['blockVersion'] === 2 ) { | |
| 45 | + return $this->build_css_v2( $attributes ); | |
| 46 | + } | |
| 47 | + return $this->build_css_v1( $attributes ); | |
| 48 | + } | |
| 49 | + | |
| 50 | + private function get_map_size_css( $attributes, $device = '' ) { | |
| 51 | + $size_css = []; | |
| 52 | + | |
| 53 | + if ( ! empty( $attributes['mapWidth'][ 'value' . $device ] ) ) { | |
| 54 | + $width_value = $attributes['mapWidth'][ 'value' . $device ]; | |
| 55 | + $width_unit = $attributes['mapWidth'][ 'valueUnit' . $device ] ?? '%'; | |
| 56 | + $size_css['width'] = $width_value . $width_unit; | |
| 57 | + } | |
| 58 | + | |
| 59 | + if ( ! empty( $attributes['mapHeight'][ 'value' . $device ] ) ) { | |
| 60 | + $height_value = $attributes['mapHeight'][ 'value' . $device ]; | |
| 61 | + $height_unit = ! empty( $attributes['mapHeight'][ 'valueUnit' . $device ] ) | |
| 62 | + ? $attributes['mapHeight'][ 'valueUnit' . $device ] | |
| 63 | + : 'px'; | |
| 64 | + | |
| 65 | + $size_css['height'] = $height_value . $height_unit; | |
| 66 | + } | |
| 67 | + | |
| 68 | + return array_merge( | |
| 69 | + Range::get_css([ | |
| 70 | + 'attributeValue' => $attributes['mapWidth'], | |
| 71 | + 'attribute_object_key' => 'value', | |
| 72 | + 'isResponsive' => true, | |
| 73 | + 'defaultValue' => 100, | |
| 74 | + 'hasUnit' => true, | |
| 75 | + 'unitDefaultValue' => '%', | |
| 76 | + 'property' => 'width', | |
| 77 | + 'device' => $device, | |
| 78 | + ]), | |
| 79 | + Range::get_css([ | |
| 80 | + 'attributeValue' => $attributes['mapHeight'], | |
| 81 | + 'attribute_object_key' => 'value', | |
| 82 | + 'isResponsive' => true, | |
| 83 | + 'defaultValue' => 500, | |
| 84 | + 'hasUnit' => true, | |
| 85 | + 'unitDefaultValue' => 'px', | |
| 86 | + 'property' => 'height', | |
| 87 | + 'device' => $device, | |
| 88 | + ]), | |
| 89 | + $size_css, | |
| 90 | + isset( $attributes['cssFilter'] ) ? CssFilter::get_css( $attributes['cssFilter'], '', $device ) : [] | |
| 91 | + ); | |
| 92 | + } | |
| 93 | + | |
| 94 | + public static function escaping_array_data( $array ) { | |
| 95 | + foreach ( $array as $key => &$value ) { | |
| 96 | + if ( is_array( $value ) ) { | |
| 97 | + $value = self::escaping_array_data( $value ); | |
| 98 | + } else { | |
| 99 | + $value = esc_attr( sanitize_text_field( $value ) ); | |
| 100 | + } | |
| 101 | + } | |
| 102 | + return $array; | |
| 103 | + } | |
| 104 | + | |
| 105 | + public function render_block_content( $attributes, $content, $block_instance ) { | |
| 106 | + $custom_icon_url = ''; | |
| 107 | + if ( isset( $attributes['iconImageUrl'] ) && ! empty( $attributes['iconImageUrl'] ) ) { | |
| 108 | + $custom_icon_url = esc_url( $attributes['iconImageUrl'] ); | |
| 109 | + } else { | |
| 110 | + $custom_icon_url = esc_url( ABLOCKS_ASSETS_URL . 'images/marker-icon.png' ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + $settings = array( | |
| 114 | + 'mapMarkerList' => $this->escaping_array_data( is_array( $attributes['mapMarkerList'] ?? null ) ? $attributes['mapMarkerList'] : [] ), | |
| 115 | + 'mapZoom' => isset( $attributes['mapZoom'] ) ? intval( $attributes['mapZoom'] ) : 10, | |
| 116 | + 'scrollWheelZoom' => isset( $attributes['scrollWheelZoom'] ) ? filter_var( $attributes['scrollWheelZoom'], FILTER_VALIDATE_BOOLEAN ) : false, | |
| 117 | + 'mapType' => isset( $attributes['mapType'] ) ? sanitize_text_field( $attributes['mapType'] ) : 'GM', | |
| 118 | + 'centerIndex' => isset( $attributes['centerIndex'] ) ? intval( $attributes['centerIndex'] ) : 0, | |
| 119 | + 'defaultMarkerIcon' => $custom_icon_url, | |
| 120 | + 'iconHeight' => isset( $attributes['iconHeight'] ) ? sanitize_text_field( $attributes['iconHeight'] ) : 40, | |
| 121 | + 'iconWidth' => isset( $attributes['iconWidth'] ) ? sanitize_text_field( $attributes['iconWidth'] ) : 25, | |
| 122 | + ); | |
| 123 | + | |
| 124 | + ob_start(); | |
| 125 | + ?> | |
| 126 | + <div | |
| 127 | + data-settings='<?php echo esc_attr( wp_json_encode( $settings, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) ); ?>' | |
| 128 | + class="ablocks-map-block" | |
| 129 | + > | |
| 130 | + </div> | |
| 131 | + <?php | |
| 132 | + $output = ob_get_clean(); | |
| 133 | + return $output; | |
| 134 | + } | |
| 135 | + | |
| 136 | +} | |