1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clubdeuce\WPGoogleMaps; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Map |
7
|
|
|
* @package Clubdeuce\WPGoogleMaps |
8
|
|
|
* |
9
|
|
|
* @method array styles() |
10
|
|
|
*/ |
11
|
|
|
class Map_Model extends Model_Base { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var null|string |
15
|
|
|
*/ |
16
|
|
|
protected $_background_color = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $_center; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The Map element height (default: 400px). |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $_height = '400px'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The string to use for the map HTML element id property |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $_html_id; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Marker[] |
38
|
|
|
*/ |
39
|
|
|
protected $_markers = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $_styles = array(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The Map element width (default: 100%). |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $_width = '100%'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var int |
54
|
|
|
*/ |
55
|
|
|
protected $_zoom = 5; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Marker $marker |
59
|
|
|
*/ |
60
|
1 |
|
public function add_marker( $marker ) { |
61
|
|
|
|
62
|
1 |
|
$this->_markers[] = $marker; |
63
|
|
|
|
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Marker[] $markers |
68
|
|
|
*/ |
69
|
1 |
|
public function add_markers( $markers ) { |
70
|
|
|
|
71
|
1 |
|
$this->_markers = array_merge( $this->_markers, $markers ); |
72
|
|
|
|
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
2 |
|
public function center() { |
79
|
|
|
|
80
|
2 |
|
return $this->_center; |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
2 |
|
public function height() { |
88
|
|
|
|
89
|
2 |
|
return $this->_height; |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
2 |
|
public function html_id() { |
97
|
|
|
|
98
|
2 |
|
if ( ! isset( $this->_html_id ) ) { |
99
|
1 |
|
$this->_html_id = sprintf( 'map-%1$s', md5( serialize( array( $this->center(), $this->markers() ) ) ) ); |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
return $this->_html_id; |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Marker[] |
108
|
|
|
*/ |
109
|
1 |
|
public function markers() { |
110
|
|
|
|
111
|
1 |
|
return $this->_markers; |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
1 |
|
public function width() { |
119
|
|
|
|
120
|
1 |
|
return $this->_width; |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
2 |
|
public function zoom() { |
128
|
|
|
|
129
|
2 |
|
return $this->_zoom; |
130
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param array $center |
135
|
|
|
*/ |
136
|
1 |
|
public function set_center( $center ) { |
137
|
|
|
|
138
|
1 |
|
$center = wp_parse_args( $center, array( |
139
|
1 |
|
'lat' => null, |
140
|
|
|
'lng' => null, |
141
|
|
|
) ); |
142
|
|
|
|
143
|
1 |
|
$this->_center = $center; |
144
|
|
|
|
145
|
1 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param int $zoom |
149
|
|
|
*/ |
150
|
1 |
|
public function set_zoom( $zoom ) { |
151
|
|
|
|
152
|
1 |
|
$this->_zoom = (int)$zoom; |
153
|
|
|
|
154
|
1 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $height |
158
|
|
|
*/ |
159
|
1 |
|
public function set_height( $height ) { |
160
|
|
|
|
161
|
1 |
|
$this->_height = $height; |
162
|
|
|
|
163
|
1 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param string $html_id |
167
|
|
|
*/ |
168
|
1 |
|
public function set_html_id( $html_id ) { |
169
|
|
|
|
170
|
1 |
|
$this->_html_id = $html_id; |
171
|
|
|
|
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param array|string $styles |
176
|
|
|
*/ |
177
|
|
|
public function set_styles( $styles ) { |
178
|
|
|
|
179
|
|
|
do { |
180
|
|
|
if ( is_string( $styles ) ) { |
181
|
|
|
$styles = json_decode( $styles, true ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ( ! is_array( $styles ) ) { |
185
|
|
|
trigger_error( __( 'The style property must be an array' ) ); |
186
|
|
|
break; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->_styles = $styles; |
190
|
|
|
} while ( false ); |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return array |
196
|
|
|
* |
197
|
|
|
* @todo Refactor to make_params |
198
|
|
|
*/ |
199
|
1 |
|
public function make_args() { |
200
|
|
|
|
201
|
|
|
return array( |
202
|
1 |
|
'center' => $this->center(), |
203
|
1 |
|
'styles' => $this->styles(), |
204
|
1 |
|
'zoom' => (int)$this->zoom(), |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
} |
210
|
|
|
|