Completed
Push — prepare/4.1 ( 0b4dc2...26fe0a )
by Jeremy
279:11 queued 269:16
created

Jetpack_Contact_Info_Widget::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
rs 9.4285
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
3
if ( ! class_exists( 'Jetpack_Contact_Info_Widget' ) ) {
4
5
	//register Contact_Info_Widget widget
6
	function jetpack_contact_info_widget_init() {
7
		register_widget( 'Jetpack_Contact_Info_Widget' );
8
	}
9
10
	add_action( 'widgets_init', 'jetpack_contact_info_widget_init' );
11
12
	/**
13
	 * Makes a custom Widget for displaying Resturant Location, Hours and Contact Info available.
14
	 *
15
	 * @package WordPress
16
	 */
17
	class Jetpack_Contact_Info_Widget extends WP_Widget {
18
19
		/**
20
		 * Constructor
21
		 */
22
		function __construct() {
23
			$widget_ops = array(
24
				'classname' => 'widget_contact_info',
25
				'description' => __( 'Display your location, hours, and contact information.', 'jetpack' ),
26
				'customize_selective_refresh' => true,
27
			);
28
			parent::__construct(
29
				'widget_contact_info',
30
				/** This filter is documented in modules/widgets/facebook-likebox.php */
31
				apply_filters( 'jetpack_widget_name', __( 'Contact Info', 'jetpack' ) ),
32
				$widget_ops
33
			);
34
			$this->alt_option_name = 'widget_contact_info';
35
36
			if ( is_customize_preview() ) {
37
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
38
			}
39
		}
40
41
		/**
42
		 * Enqueue scripts and styles.
43
		 */
44
		public function enqueue_scripts() {
45
			wp_enqueue_script( 'jquery' );
46
			wp_enqueue_script( 'google-maps', 'https://maps.googleapis.com/maps/api/js?sensor=false' );
47
			wp_enqueue_script( 'contact-info-map-js', plugins_url( 'contact-info/contact-info-map.js', __FILE__ ), array( 'jquery', 'google-maps' ), 20150127 );
48
			wp_enqueue_style( 'contact-info-map-css', plugins_url( 'contact-info/contact-info-map.css', __FILE__ ), null, 20150127 );
49
		}
50
51
		/**
52
		 * Return an associative array of default values
53
		 *
54
		 * These values are used in new widgets.
55
		 *
56
		 * @return array Array of default values for the Widget's options
57
		 */
58
		public function defaults() {
59
			return array(
60
				'title'   => __( 'Hours & Info', 'jetpack' ),
61
				'address' => __( "3999 Mission Boulevard,\nSan Diego CA 92109", 'jetpack' ),
62
				'phone'   => _x( '1-202-555-1212', 'Example of a phone number', 'jetpack' ),
63
				'hours'   => __( "Lunch: 11am - 2pm \nDinner: M-Th 5pm - 11pm, Fri-Sat:5pm - 1am", 'jetpack' ),
64
				'showmap' => 1,
65
				'lat'     => null,
66
				'lon'     => null
67
			);
68
		}
69
70
		/**
71
		 * Outputs the HTML for this widget.
72
		 *
73
		 * @param array $args     An array of standard parameters for widgets in this theme
74
		 * @param array $instance An array of settings for this widget instance
75
		 *
76
		 * @return void Echoes it's output
77
		 **/
78
		function widget( $args, $instance ) {
79
			$instance = wp_parse_args( $instance, $this->defaults() );
80
81
			echo $args['before_widget'];
82
83
			if ( '' != $instance['title'] ) {
84
				echo $args['before_title'] . $instance['title'] . $args['after_title'];
85
			}
86
87
			/**
88
			 * Fires at the beginning of the Contact Info widget, after the title.
89
			 *
90
			 * @module widgets
91
			 *
92
			 * @since 3.9.2
93
			 */
94
			do_action( 'jetpack_contact_info_widget_start' );
95
96
			if ( '' != $instance['address'] ) {
97
98
				$showmap = $instance['showmap'];
99
100
				if ( $showmap && $this->has_good_map( $instance ) ) {
101
102
					$lat = $instance['lat'];
103
					$lon = $instance['lon'];
104
105
					echo $this->build_map( $lat, $lon );
106
				}
107
108
				$map_link = $this->build_map_link( $instance['address'] );
109
110
				echo '<div class="confit-address"><a href="' . esc_url( $map_link ) . '" target="_blank">' . str_replace( "\n", "<br/>", esc_html( $instance['address'] ) ) . "</a></div>";
111
			}
112
113
			if ( '' != $instance['phone'] ) {
114
				if ( wp_is_mobile() ) {
115
					echo '<div class="confit-phone"><a href="' . esc_url( 'tel:' . $instance['phone'] ) . '">' . esc_html( $instance['phone'] ) . "</a></div>";
116
				}
117
				else {
118
					echo '<div class="confit-phone">' . esc_html( $instance['phone'] ) . '</div>';
119
				}
120
			}
121
122
			if ( '' != $instance['hours'] ) {
123
				echo '<div class="confit-hours">' . str_replace( "\n", "<br/>", esc_html( $instance['hours'] ) ) . "</div>";
124
			}
125
126
			/**
127
			 * Fires at the end of Contact Info widget.
128
			 *
129
			 * @module widgets
130
			 *
131
			 * @since 3.9.2
132
			 */
133
			do_action( 'jetpack_contact_info_widget_end' );
134
135
			echo $args['after_widget'];
136
		}
137
138
139
		/**
140
		 * Deals with the settings when they are saved by the admin. Here is
141
		 * where any validation should be dealt with.
142
		 *
143
		 * @param array $new_instance New configuration values
144
		 * @param array $old_instance Old configuration values
145
		 *
146
		 * @return array
147
		 */
148
		function update( $new_instance, $old_instance ) {
149
			$update_lat_lon = false;
150
			if (
151
				! isset( $old_instance['address'] ) ||
152
				$this->urlencode_address( $old_instance['address'] ) != $this->urlencode_address( $new_instance['address'] )
153
			) {
154
				$update_lat_lon = true;
155
			}
156
157
			$instance            = array();
158
			$instance['title']   = wp_kses( $new_instance['title'], array() );
159
			$instance['address'] = wp_kses( $new_instance['address'], array() );
160
			$instance['phone']   = wp_kses( $new_instance['phone'], array() );
161
			$instance['hours']   = wp_kses( $new_instance['hours'], array() );
162
			$instance['lat']     = isset( $old_instance['lat'] ) ? floatval( $old_instance['lat'] ) : 0;
163
			$instance['lon']     = isset( $old_instance['lon'] ) ? floatval( $old_instance['lon'] ) : 0;
164
165
			if ( ! $instance['lat'] || ! $instance['lon'] ) {
166
				$update_lat_lon = true;
167
			}
168
169
			if ( $instance['address'] && $update_lat_lon ) {
170
171
				// Get the lat/lon of the user specified address.
172
				$address = $this->urlencode_address( $instance['address'] );
173
				$path    = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . $address;
174
				$json    = wp_remote_retrieve_body( wp_remote_get( $path ) );
175
176
				if ( ! $json ) {
177
					// The read failed :(
178
					esc_html_e( "There was a problem getting the data to display this address on a map.  Please refresh your browser and try again.", 'jetpack' );
179
					die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method update() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
180
				}
181
182
				$json_obj = json_decode( $json );
183
184
				if ( "ZERO_RESULTS" == $json_obj->status ) {
185
					// The address supplied does not have a matching lat / lon.
186
					// No map is available.
187
					$instance['lat'] = "0";
188
					$instance['lon'] = "0";
189
				}
190
				else {
191
192
					$loc = $json_obj->results[0]->geometry->location;
193
194
					$lat = floatval( $loc->lat );
195
					$lon = floatval( $loc->lng );
196
197
					$instance['lat'] = "$lat";
198
					$instance['lon'] = "$lon";
199
				}
200
			}
201
202
			if ( ! isset( $new_instance['showmap'] ) ) {
203
				$instance['showmap'] = 0;
204
			}
205
			else {
206
				$instance['showmap'] = intval( $new_instance['showmap'] );
207
			}
208
209
			return $instance;
210
		}
211
212
213
		/**
214
		 * Displays the form for this widget on the Widgets page of the WP Admin area.
215
		 *
216
		 * @param array $instance Instance configuration.
217
		 *
218
		 * @return void
219
		 */
220
		function form( $instance ) {
221
			$instance = wp_parse_args( $instance, $this->defaults() );
222
			?>
223
			<p>
224
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
225
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
226
			</p>
227
228
			<p>
229
				<label for="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>"><?php esc_html_e( 'Address:', 'jetpack' ); ?></label>
230
				<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'address' ) ); ?>"><?php echo esc_textarea( $instance['address'] ); ?></textarea>
231
				<?php
232
				if ( $this->has_good_map( $instance ) ) {
233
					?>
234
					<input class="" id="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'showmap' ) ); ?>" value="1" type="checkbox" <?php checked( $instance['showmap'], 1 ); ?> />
235
					<label for="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>"><?php esc_html_e( 'Show map', 'jetpack' ); ?></label>
236
					<?php
237
				}
238
				else {
239
					?>
240
					<span class="error-message"><?php _e( 'Sorry. We can not plot this address. A map will not be displayed. Is the address formatted correctly?', 'jetpack' ); ?></span>
241
					<input id="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'showmap' ) ); ?>" value="<?php echo( intval( $instance['showmap'] ) ); ?>" type="hidden" />
242
					<?php
243
				}
244
				?>
245
			</p>
246
			<p>
247
				<label for="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>"><?php esc_html_e( 'Phone:', 'jetpack' ); ?></label>
248
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'phone' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['phone'] ); ?>" />
249
			</p>
250
251
			<p>
252
				<label for="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>"><?php esc_html_e( 'Hours:', 'jetpack' ); ?></label>
253
				<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hours' ) ); ?>"><?php echo esc_textarea( $instance['hours'] ); ?></textarea>
254
			</p>
255
256
			<?php
257
		}
258
259
260
		/**
261
		 * Generate a Google Maps link for the supplied address.
262
		 *
263
		 * @param string $address Address to link to.
264
		 *
265
		 * @return string
266
		 */
267
		function build_map_link( $address ) {
268
			// Google map urls have lots of available params but zoom (z) and query (q) are enough.
269
			return "http://maps.google.com/maps?z=16&q=" . $this->urlencode_address( $address );
270
		}
271
272
273
		/**
274
		 * Builds map display HTML code from the supplied latitude and longitude.
275
		 *
276
		 * @param float $lat Map Latitude
277
		 * @param float $lon Map Longitude
278
		 *
279
		 * @return string HTML of the map
280
		 */
281
		function build_map( $lat, $lon ) {
282
			$this->enqueue_scripts();
283
284
			$lat  = esc_attr( $lat );
285
			$lon  = esc_attr( $lon );
286
			$html = <<<EOT
287
				<div class="contact-map">
288
				<input type="hidden" class="contact-info-map-lat" value="$lat" />
289
				<input type="hidden" class="contact-info-map-lon" value="$lon" />
290
				<div class="contact-info-map-canvas"></div></div>
291
EOT;
292
293
			return $html;
294
		}
295
296
		/**
297
		 * Encode an URL
298
		 *
299
		 * @param string $address The URL to encode
300
		 *
301
		 * @return string The encoded URL
302
		 */
303
		function urlencode_address( $address ) {
304
305
			$address = strtolower( $address );
306
			$address = preg_replace( "/\s+/", " ", trim( $address ) ); // Get rid of any unwanted whitespace
307
			$address = str_ireplace( " ", "+", $address ); // Use + not %20
308
			urlencode( $address );
309
310
			return $address;
311
		}
312
313
		/**
314
		 * Check if the instance has a valid Map location.
315
		 *
316
		 * @param array $instance Widget instance configuration.
317
		 *
318
		 * @return bool Whether or not there is a valid map.
319
		 */
320
		function has_good_map( $instance ) {
321
			// The lat and lon of an address that could not be plotted will have values of 0 and 0.
322
			return ! ( "0" == $instance['lat'] && "0" == $instance['lon'] );
323
		}
324
325
	}
326
327
}
328