google-maps.php (6619B)
1 <?php 2 namespace Elementor; 3 4 use Elementor\Modules\DynamicTags\Module as TagsModule; 5 6 if ( ! defined( 'ABSPATH' ) ) { 7 exit; // Exit if accessed directly. 8 } 9 10 /** 11 * Elementor google maps widget. 12 * 13 * Elementor widget that displays an embedded google map. 14 * 15 * @since 1.0.0 16 */ 17 class Widget_Google_Maps extends Widget_Base { 18 19 /** 20 * Get widget name. 21 * 22 * Retrieve google maps widget name. 23 * 24 * @since 1.0.0 25 * @access public 26 * 27 * @return string Widget name. 28 */ 29 public function get_name() { 30 return 'google_maps'; 31 } 32 33 /** 34 * Get widget title. 35 * 36 * Retrieve google maps widget title. 37 * 38 * @since 1.0.0 39 * @access public 40 * 41 * @return string Widget title. 42 */ 43 public function get_title() { 44 return esc_html__( 'Google Maps', 'elementor' ); 45 } 46 47 /** 48 * Get widget icon. 49 * 50 * Retrieve google maps widget icon. 51 * 52 * @since 1.0.0 53 * @access public 54 * 55 * @return string Widget icon. 56 */ 57 public function get_icon() { 58 return 'eicon-google-maps'; 59 } 60 61 /** 62 * Get widget categories. 63 * 64 * Retrieve the list of categories the google maps widget belongs to. 65 * 66 * Used to determine where to display the widget in the editor. 67 * 68 * @since 2.0.0 69 * @access public 70 * 71 * @return array Widget categories. 72 */ 73 public function get_categories() { 74 return [ 'basic' ]; 75 } 76 77 /** 78 * Get widget keywords. 79 * 80 * Retrieve the list of keywords the widget belongs to. 81 * 82 * @since 2.1.0 83 * @access public 84 * 85 * @return array Widget keywords. 86 */ 87 public function get_keywords() { 88 return [ 'google', 'map', 'embed', 'location' ]; 89 } 90 91 /** 92 * Register google maps widget controls. 93 * 94 * Adds different input fields to allow the user to change and customize the widget settings. 95 * 96 * @since 3.1.0 97 * @access protected 98 */ 99 protected function register_controls() { 100 $this->start_controls_section( 101 'section_map', 102 [ 103 'label' => esc_html__( 'Map', 'elementor' ), 104 ] 105 ); 106 107 if ( Plugin::$instance->editor->is_edit_mode() ) { 108 $api_key = get_option( 'elementor_google_maps_api_key' ); 109 110 if ( ! $api_key ) { 111 $this->add_control( 112 'api_key_notification', 113 [ 114 'type' => Controls_Manager::RAW_HTML, 115 'raw' => sprintf( 116 esc_html__( 'Set your Google Maps API Key in Elementor\'s <a href="%1$s" target="_blank">Integrations Settings</a> page. Create your key <a href="%2$s" target="_blank">here.', 'elementor' ), 117 Settings::get_url() . '#tab-integrations', 118 'https://developers.google.com/maps/documentation/embed/get-api-key' 119 ), 120 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info', 121 ] 122 ); 123 } 124 } 125 126 $default_address = esc_html__( 'London Eye, London, United Kingdom', 'elementor' ); 127 $this->add_control( 128 'address', 129 [ 130 'label' => esc_html__( 'Location', 'elementor' ), 131 'type' => Controls_Manager::TEXT, 132 'dynamic' => [ 133 'active' => true, 134 'categories' => [ 135 TagsModule::POST_META_CATEGORY, 136 ], 137 ], 138 'placeholder' => $default_address, 139 'default' => $default_address, 140 'label_block' => true, 141 ] 142 ); 143 144 $this->add_control( 145 'zoom', 146 [ 147 'label' => esc_html__( 'Zoom', 'elementor' ), 148 'type' => Controls_Manager::SLIDER, 149 'default' => [ 150 'size' => 10, 151 ], 152 'range' => [ 153 'px' => [ 154 'min' => 1, 155 'max' => 20, 156 ], 157 ], 158 'separator' => 'before', 159 ] 160 ); 161 162 $this->add_responsive_control( 163 'height', 164 [ 165 'label' => esc_html__( 'Height', 'elementor' ), 166 'type' => Controls_Manager::SLIDER, 167 'range' => [ 168 'px' => [ 169 'min' => 40, 170 'max' => 1440, 171 ], 172 'vh' => [ 173 'min' => 0, 174 'max' => 100, 175 ], 176 ], 177 'size_units' => [ 'px', 'vh' ], 178 'selectors' => [ 179 '{{WRAPPER}} iframe' => 'height: {{SIZE}}{{UNIT}};', 180 ], 181 ] 182 ); 183 184 $this->add_control( 185 'view', 186 [ 187 'label' => esc_html__( 'View', 'elementor' ), 188 'type' => Controls_Manager::HIDDEN, 189 'default' => 'traditional', 190 ] 191 ); 192 193 $this->end_controls_section(); 194 195 $this->start_controls_section( 196 'section_map_style', 197 [ 198 'label' => esc_html__( 'Map', 'elementor' ), 199 'tab' => Controls_Manager::TAB_STYLE, 200 ] 201 ); 202 203 $this->start_controls_tabs( 'map_filter' ); 204 205 $this->start_controls_tab( 'normal', 206 [ 207 'label' => esc_html__( 'Normal', 'elementor' ), 208 ] 209 ); 210 211 $this->add_group_control( 212 Group_Control_Css_Filter::get_type(), 213 [ 214 'name' => 'css_filters', 215 'selector' => '{{WRAPPER}} iframe', 216 ] 217 ); 218 219 $this->end_controls_tab(); 220 221 $this->start_controls_tab( 'hover', 222 [ 223 'label' => esc_html__( 'Hover', 'elementor' ), 224 ] 225 ); 226 227 $this->add_group_control( 228 Group_Control_Css_Filter::get_type(), 229 [ 230 'name' => 'css_filters_hover', 231 'selector' => '{{WRAPPER}}:hover iframe', 232 ] 233 ); 234 235 $this->add_control( 236 'hover_transition', 237 [ 238 'label' => esc_html__( 'Transition Duration', 'elementor' ), 239 'type' => Controls_Manager::SLIDER, 240 'range' => [ 241 'px' => [ 242 'max' => 3, 243 'step' => 0.1, 244 ], 245 ], 246 'selectors' => [ 247 '{{WRAPPER}} iframe' => 'transition-duration: {{SIZE}}s', 248 ], 249 ] 250 ); 251 252 $this->end_controls_tab(); 253 254 $this->end_controls_tabs(); 255 256 $this->end_controls_section(); 257 } 258 259 /** 260 * Render google maps widget output on the frontend. 261 * 262 * Written in PHP and used to generate the final HTML. 263 * 264 * @since 1.0.0 265 * @access protected 266 */ 267 protected function render() { 268 $settings = $this->get_settings_for_display(); 269 270 if ( empty( $settings['address'] ) ) { 271 return; 272 } 273 274 if ( 0 === absint( $settings['zoom']['size'] ) ) { 275 $settings['zoom']['size'] = 10; 276 } 277 278 $api_key = esc_html( get_option( 'elementor_google_maps_api_key' ) ); 279 280 $params = [ 281 rawurlencode( $settings['address'] ), 282 absint( $settings['zoom']['size'] ), 283 ]; 284 285 if ( $api_key ) { 286 $params[] = $api_key; 287 288 $url = 'https://www.google.com/maps/embed/v1/place?key=%3$s&q=%1$s&zoom=%2$d'; 289 } else { 290 $url = 'https://maps.google.com/maps?q=%1$s&t=m&z=%2$d&output=embed&iwloc=near'; 291 } 292 293 ?> 294 <div class="elementor-custom-embed"> 295 <iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" 296 src="<?php echo esc_url( vsprintf( $url, $params ) ); ?>" 297 title="<?php echo esc_attr( $settings['address'] ); ?>" 298 aria-label="<?php echo esc_attr( $settings['address'] ); ?>" 299 ></iframe> 300 </div> 301 <?php 302 } 303 304 /** 305 * Render google maps widget output in the editor. 306 * 307 * Written as a Backbone JavaScript template and used to generate the live preview. 308 * 309 * @since 2.9.0 310 * @access protected 311 */ 312 protected function content_template() {} 313 }