| 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 |
/* translators: 1: Integration settings link open tag, 2: Create API key link open tag, 3: Link close tag. */ |
| 117 |
esc_html__( 'Set your Google Maps API Key in Elementor\'s %1$sIntegrations Settings%3$s page. Create your key %2$shere.%3$s', 'elementor' ), |
| 118 |
'<a href="' . Settings::get_url() . '#tab-integrations" target="_blank">', |
| 119 |
'<a href="https://developers.google.com/maps/documentation/embed/get-api-key" target="_blank">', |
| 120 |
'</a>' |
| 121 |
), |
| 122 |
'content_classes' => 'elementor-panel-alert elementor-panel-alert-info', |
| 123 |
] |
| 124 |
); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
$default_address = esc_html__( 'London Eye, London, United Kingdom', 'elementor' ); |
| 129 |
$this->add_control( |
| 130 |
'address', |
| 131 |
[ |
| 132 |
'label' => esc_html__( 'Location', 'elementor' ), |
| 133 |
'type' => Controls_Manager::TEXT, |
| 134 |
'dynamic' => [ |
| 135 |
'active' => true, |
| 136 |
'categories' => [ |
| 137 |
TagsModule::POST_META_CATEGORY, |
| 138 |
], |
| 139 |
], |
| 140 |
'placeholder' => $default_address, |
| 141 |
'default' => $default_address, |
| 142 |
'label_block' => true, |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
$this->add_control( |
| 147 |
'zoom', |
| 148 |
[ |
| 149 |
'label' => esc_html__( 'Zoom', 'elementor' ), |
| 150 |
'type' => Controls_Manager::SLIDER, |
| 151 |
'default' => [ |
| 152 |
'size' => 10, |
| 153 |
], |
| 154 |
'range' => [ |
| 155 |
'px' => [ |
| 156 |
'min' => 1, |
| 157 |
'max' => 20, |
| 158 |
], |
| 159 |
], |
| 160 |
'separator' => 'before', |
| 161 |
] |
| 162 |
); |
| 163 |
|
| 164 |
$this->add_responsive_control( |
| 165 |
'height', |
| 166 |
[ |
| 167 |
'label' => esc_html__( 'Height', 'elementor' ), |
| 168 |
'type' => Controls_Manager::SLIDER, |
| 169 |
'range' => [ |
| 170 |
'px' => [ |
| 171 |
'min' => 40, |
| 172 |
'max' => 1440, |
| 173 |
], |
| 174 |
'vh' => [ |
| 175 |
'min' => 0, |
| 176 |
'max' => 100, |
| 177 |
], |
| 178 |
], |
| 179 |
'size_units' => [ 'px', 'vh' ], |
| 180 |
'selectors' => [ |
| 181 |
'{{WRAPPER}} iframe' => 'height: {{SIZE}}{{UNIT}};', |
| 182 |
], |
| 183 |
] |
| 184 |
); |
| 185 |
|
| 186 |
$this->add_control( |
| 187 |
'view', |
| 188 |
[ |
| 189 |
'label' => esc_html__( 'View', 'elementor' ), |
| 190 |
'type' => Controls_Manager::HIDDEN, |
| 191 |
'default' => 'traditional', |
| 192 |
] |
| 193 |
); |
| 194 |
|
| 195 |
$this->end_controls_section(); |
| 196 |
|
| 197 |
$this->start_controls_section( |
| 198 |
'section_map_style', |
| 199 |
[ |
| 200 |
'label' => esc_html__( 'Map', 'elementor' ), |
| 201 |
'tab' => Controls_Manager::TAB_STYLE, |
| 202 |
] |
| 203 |
); |
| 204 |
|
| 205 |
$this->start_controls_tabs( 'map_filter' ); |
| 206 |
|
| 207 |
$this->start_controls_tab( 'normal', |
| 208 |
[ |
| 209 |
'label' => esc_html__( 'Normal', 'elementor' ), |
| 210 |
] |
| 211 |
); |
| 212 |
|
| 213 |
$this->add_group_control( |
| 214 |
Group_Control_Css_Filter::get_type(), |
| 215 |
[ |
| 216 |
'name' => 'css_filters', |
| 217 |
'selector' => '{{WRAPPER}} iframe', |
| 218 |
] |
| 219 |
); |
| 220 |
|
| 221 |
$this->end_controls_tab(); |
| 222 |
|
| 223 |
$this->start_controls_tab( 'hover', |
| 224 |
[ |
| 225 |
'label' => esc_html__( 'Hover', 'elementor' ), |
| 226 |
] |
| 227 |
); |
| 228 |
|
| 229 |
$this->add_group_control( |
| 230 |
Group_Control_Css_Filter::get_type(), |
| 231 |
[ |
| 232 |
'name' => 'css_filters_hover', |
| 233 |
'selector' => '{{WRAPPER}}:hover iframe', |
| 234 |
] |
| 235 |
); |
| 236 |
|
| 237 |
$this->add_control( |
| 238 |
'hover_transition', |
| 239 |
[ |
| 240 |
'label' => esc_html__( 'Transition Duration', 'elementor' ), |
| 241 |
'type' => Controls_Manager::SLIDER, |
| 242 |
'range' => [ |
| 243 |
'px' => [ |
| 244 |
'max' => 3, |
| 245 |
'step' => 0.1, |
| 246 |
], |
| 247 |
], |
| 248 |
'selectors' => [ |
| 249 |
'{{WRAPPER}} iframe' => 'transition-duration: {{SIZE}}s', |
| 250 |
], |
| 251 |
] |
| 252 |
); |
| 253 |
|
| 254 |
$this->end_controls_tab(); |
| 255 |
|
| 256 |
$this->end_controls_tabs(); |
| 257 |
|
| 258 |
$this->end_controls_section(); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Render google maps widget output on the frontend. |
| 263 |
* |
| 264 |
* Written in PHP and used to generate the final HTML. |
| 265 |
* |
| 266 |
* @since 1.0.0 |
| 267 |
* @access protected |
| 268 |
*/ |
| 269 |
protected function render() { |
| 270 |
$settings = $this->get_settings_for_display(); |
| 271 |
|
| 272 |
if ( empty( $settings['address'] ) ) { |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
if ( 0 === absint( $settings['zoom']['size'] ) ) { |
| 277 |
$settings['zoom']['size'] = 10; |
| 278 |
} |
| 279 |
|
| 280 |
$api_key = esc_html( get_option( 'elementor_google_maps_api_key' ) ); |
| 281 |
|
| 282 |
$params = [ |
| 283 |
rawurlencode( $settings['address'] ), |
| 284 |
absint( $settings['zoom']['size'] ), |
| 285 |
]; |
| 286 |
|
| 287 |
if ( $api_key ) { |
| 288 |
$params[] = $api_key; |
| 289 |
|
| 290 |
$url = 'https://www.google.com/maps/embed/v1/place?key=%3$s&q=%1$s&zoom=%2$d'; |
| 291 |
} else { |
| 292 |
$url = 'https://maps.google.com/maps?q=%1$s&t=m&z=%2$d&output=embed&iwloc=near'; |
| 293 |
} |
| 294 |
|
| 295 |
?> |
| 296 |
<div class="elementor-custom-embed"> |
| 297 |
<iframe frameborder="0" scrolling="no" marginheight="0" marginwidth="0" |
| 298 |
src="<?php echo esc_url( vsprintf( $url, $params ) ); ?>" |
| 299 |
title="<?php echo esc_attr( $settings['address'] ); ?>" |
| 300 |
aria-label="<?php echo esc_attr( $settings['address'] ); ?>" |
| 301 |
></iframe> |
| 302 |
</div> |
| 303 |
<?php |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Render google maps widget output in the editor. |
| 308 |
* |
| 309 |
* Written as a Backbone JavaScript template and used to generate the live preview. |
| 310 |
* |
| 311 |
* @since 2.9.0 |
| 312 |
* @access protected |
| 313 |
*/ |
| 314 |
protected function content_template() {} |
| 315 |
} |
| 316 |
|