1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clubdeuce\WPLib\Components\GoogleMaps; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Marker_Model |
7
|
|
|
* @package Clubdeuce\WPLib\Components\GoogleMaps |
8
|
|
|
*/ |
9
|
|
|
class Marker_Model extends \WPLib_Model_Base { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $_address; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var Geocoder |
18
|
|
|
*/ |
19
|
|
|
protected $_geocoder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Info_Window |
23
|
|
|
*/ |
24
|
|
|
protected $_info_window; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Marker_Label |
28
|
|
|
*/ |
29
|
|
|
protected $_label; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var double |
33
|
|
|
*/ |
34
|
|
|
protected $_latitude = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var double |
38
|
|
|
*/ |
39
|
|
|
protected $_longitude; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Location|\WP_Error |
43
|
|
|
*/ |
44
|
|
|
protected $_location; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $_title; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Marker_Model constructor. |
53
|
|
|
* @param array $args |
54
|
|
|
*/ |
55
|
1 |
|
function __construct( $args = array() ) { |
56
|
|
|
|
57
|
1 |
|
$args = wp_parse_args( $args, array( |
58
|
1 |
|
'address' => '', |
59
|
1 |
|
'info_window' => '', |
60
|
1 |
|
'label' => new Marker_Label(), |
61
|
1 |
|
'title' => '', |
62
|
|
|
'latitude' => null, |
63
|
|
|
'longitude' => null, |
64
|
|
|
) ); |
65
|
|
|
|
66
|
1 |
|
if ( empty( $args['info_window'] ) ) { |
67
|
1 |
|
$args['info_window'] = new Info_Window( array( |
68
|
1 |
|
'position' => array( 'lat' => $args['latitude'], 'lng' => $args['longitude'] ), |
69
|
|
|
) ); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
parent::__construct( $args ); |
73
|
|
|
|
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Info_Window |
78
|
|
|
*/ |
79
|
|
|
function info_window() { |
80
|
|
|
|
81
|
|
|
return $this->_info_window; |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return Marker_Label |
87
|
|
|
*/ |
88
|
1 |
|
function label() { |
89
|
|
|
|
90
|
1 |
|
if ( is_string( $this->_label ) ) { |
91
|
|
|
$this->_label = new Marker_Label( array( 'text' => $this->label() ) ); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
return $this->_label; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return double |
100
|
|
|
*/ |
101
|
2 |
View Code Duplication |
function latitude() { |
|
|
|
|
102
|
|
|
|
103
|
2 |
|
if ( is_null( $this->_latitude ) && ! is_wp_error( $this->location() ) ) { |
104
|
2 |
|
$this->_latitude = $this->location()->latitude(); |
105
|
|
|
} |
106
|
2 |
|
return doubleval( $this->_latitude ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return Location|\WP_Error |
111
|
|
|
*/ |
112
|
2 |
|
function location() { |
113
|
2 |
|
if ( ! is_object( $this->_location ) ) { |
114
|
2 |
|
$this->_location = $this->_geocoder()->geocode( $this->_address ); |
115
|
|
|
} |
116
|
2 |
|
return $this->_location; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return double |
121
|
|
|
*/ |
122
|
2 |
View Code Duplication |
function longitude() { |
|
|
|
|
123
|
|
|
|
124
|
2 |
|
if ( is_null( $this->_longitude ) && ! is_wp_error( $this->location() ) ) { |
125
|
2 |
|
$this->_longitude = doubleval( $this->location()->longitude() ); |
126
|
|
|
} |
127
|
2 |
|
return doubleval( $this->_longitude ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the position of this marker. An array with key/value pairs of lat and lng. |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
2 |
|
function position() { |
136
|
|
|
|
137
|
2 |
|
return array( 'lat' => $this->latitude(), 'lng' => $this->longitude() ); |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
2 |
|
function title() { |
145
|
2 |
|
return $this->_title; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array $args |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
1 |
|
function marker_args( $args = array() ) { |
153
|
|
|
|
154
|
1 |
|
$args = array_merge( $args, $this->extra_args ); |
155
|
|
|
|
156
|
1 |
|
$args = wp_parse_args( $args, array( |
157
|
1 |
|
'position' => $this->position(), |
158
|
1 |
|
'label' => $this->label()->options(), |
159
|
1 |
|
'title' => $this->title(), |
160
|
|
|
) ); |
161
|
|
|
|
162
|
1 |
|
return $args; |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return Geocoder |
168
|
|
|
*/ |
169
|
3 |
|
private function _geocoder() { |
170
|
3 |
|
if (! is_a( $this->_geocoder, '\Clubdeuce\WPLib\Components\GoogleMaps\Geocoder' ) ) { |
171
|
1 |
|
$this->_geocoder = new Geocoder(); |
172
|
|
|
} |
173
|
|
|
|
174
|
3 |
|
return $this->_geocoder; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
} |
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.