Completed
Push — add/actions-contact-info-widge... ( 7d468c )
by Jeremy
11:15
created

Jetpack_Contact_Info_Widget::widget()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 67
Code Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 67
rs 6.6523
cc 8
eloc 25
nc 36
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
23
		 **/
24 View Code Duplication
		function __construct() {
25
			$widget_ops = array(
26
				'classname' => 'widget_contact_info',
27
				'description' => __( 'Display your location, hours, and contact information.', 'jetpack' )
28
			);
29
			parent::__construct(
30
				'widget_contact_info',
31
				/** This filter is documented in modules/widgets/facebook-likebox.php */
32
				apply_filters( 'jetpack_widget_name', __( 'Contact Info', 'jetpack' ) ),
33
				$widget_ops
34
			);
35
			$this->alt_option_name = 'widget_contact_info';
36
		}
37
38
39
		/**
40
		 * Return an associative array of default values
41
		 *
42
		 * These values are used in new widgets.
43
		 *
44
		 * @return array Array of default values for the Widget's options
45
		 */
46
		public function defaults() {
47
			return array(
48
				'title'   => __( 'Hours & Info', 'jetpack' ),
49
				'address' => __( "3999 Mission Boulevard,\nSan Diego CA 92109", 'jetpack' ),
50
				'phone'   => _x( '1-202-555-1212', 'Example of a phone number', 'jetpack' ),
51
				'hours'   => __( "Lunch: 11am - 2pm \nDinner: M-Th 5pm - 11pm, Fri-Sat:5pm - 1am", 'jetpack' ),
52
				'showmap' => 1,
53
				'lat'     => null,
54
				'lon'     => null
55
			);
56
		}
57
		/**
58
		 * Outputs the HTML for this widget.
59
		 *
60
		 * @param array An array of standard parameters for widgets in this theme
61
		 * @param array An array of settings for this widget instance
62
		 * @return void Echoes it's output
63
		 **/
64
		function widget( $args, $instance ) {
65
			$instance = wp_parse_args( $instance, $this->defaults() );
66
67
			extract( $args, EXTR_SKIP );
68
69
			echo $before_widget;
70
71
			if ( $instance['title'] != '' )
72
				echo $before_title . $instance['title'] . $after_title;
73
74
			/**
75
			 * Fires at the beginning of the Contact Info widget, after the title.
76
			 *
77
			 * @since 3.9.2
78
			 */
79
			do_action( 'jetpack_contact_info_widget_beginning' );
80
81
			$map_link = 0;
0 ignored issues
show
Unused Code introduced by
$map_link is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
83
84
			if ( $instance['address'] != '' ) {
85
86
				$showmap = $instance['showmap'];
87
88
				if ( $showmap && $this->has_good_map( $instance ) ) {
89
90
					$lat = $instance['lat'];
91
					$lon = $instance['lon'];
92
93
					echo $this->build_map( $lat, $lon );
94
				}
95
96
				$map_link = $this->build_map_link( $instance['address'] );
97
98
				echo '<div class="confit-address"><a href="' . esc_url( $map_link ) . '" target="_blank">' . str_replace( "\n", "<br/>", esc_html( $instance['address'] ) ) . "</a></div>";
99
100
101
			}
102
103
104
			if ( $instance['phone'] != '' ) {
105
106
				if( wp_is_mobile() ) {
107
					echo '<div class="confit-phone"><a href="'. esc_url( 'tel:'. $instance['phone'] ) . '">' . esc_html( $instance['phone'] ) . "</a></div>";
108
				} else {
109
					echo '<div class="confit-phone">' . esc_html( $instance['phone'] ) . '</div>';
110
				}
111
112
			}
113
114
115
			if ( $instance['hours'] != '' ) {
116
				echo '<div class="confit-hours">' . str_replace( "\n", "<br/>", esc_html( $instance['hours'] ) ) . "</div>";
117
			}
118
119
120
			/**
121
			 * Fires at the end of Contact Info widget.
122
			 *
123
			 * @since 3.9.2
124
			 */
125
			do_action( 'jetpack_contact_info_widget_end' );
126
127
128
			echo $after_widget;
129
130
		}
131
132
133
		/**
134
		 * Deals with the settings when they are saved by the admin. Here is
135
		 * where any validation should be dealt with.
136
		 **/
137
		function update( $new_instance, $old_instance ) {
138
			$update_lat_lon = false;
139
			if ( $this->urlencode_address( $old_instance['address'] ) != $this->urlencode_address( $new_instance['address'] ) ) {
140
				$update_lat_lon = true;
141
			}
142
143
			$instance = array();
144
			$instance['title'] = wp_kses( $new_instance['title'], array() );
145
			$instance['address'] = wp_kses( $new_instance['address'], array() );
146
			$instance['phone'] = wp_kses( $new_instance['phone'], array() );
147
			$instance['hours'] = wp_kses( $new_instance['hours'], array() );
148
			$instance['lat'] = isset( $old_instance['lat'] ) ? floatval( $old_instance['lat'] ) : 0;
149
			$instance['lon'] = isset( $old_instance['lon'] ) ? floatval( $old_instance['lon'] ) : 0;
150
151
			if ( ! $instance['lat'] || ! $instance['lon'] ) {
152
				$update_lat_lon = true;
153
			}
154
155
			if ( $instance['address'] && $update_lat_lon ) {
156
157
				// Get the lat/lon of the user specified address.
158
				$address = $this->urlencode_address( $instance['address'] );
159
				$path = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . $address;
160
				$json = wp_remote_retrieve_body( wp_remote_get( $path ) );
161
162
				if ( ! $json ) {
163
					// The read failed :(
164
					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' );
165
					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...
166
				}
167
168
				$json_obj = json_decode( $json );
169
170
				if ( $err = $json_obj->status == "ZERO_RESULTS" ) {
0 ignored issues
show
Unused Code introduced by
$err is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
171
					// The address supplied does not have a matching lat / lon.
172
					// No map is available.
173
					$instance['lat'] = "0";
174
					$instance['lon'] = "0";
175
				} else {
176
177
					$loc = $json_obj->results[0]->geometry->location;
178
179
					$lat = floatval( $loc->lat );
180
					$lon = floatval( $loc->lng );
181
182
					$instance['lat'] = "$lat";
183
					$instance['lon'] = "$lon";
184
				}
185
			}
186
187
			if ( ! isset( $new_instance['showmap'] ) ) {
188
				$instance['showmap'] = 0;
189
			} else {
190
				$instance['showmap'] = intval( $new_instance['showmap'] );
191
			}
192
193
			return $instance;
194
		}
195
196
197
		/**
198
		 * Displays the form for this widget on the Widgets page of the WP Admin area.
199
		 **/
200
		function form( $instance ) {
201
			$instance = wp_parse_args( $instance, $this->defaults() );
202
			extract( $instance );
203
204
			$disabled = !$this->has_good_map( $instance );
0 ignored issues
show
Unused Code introduced by
$disabled is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
205
	?>
206
				<p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
207
208
				<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( $title ); ?>" /></p>
209
210
				<p><label for="<?php echo esc_attr( $this->get_field_id( 'address' ) ); ?>"><?php esc_html_e( 'Address:', 'jetpack' ); ?></label>
211
				<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( $address ); ?></textarea>
212
	<?php
213
			if ( $this->has_good_map( $instance ) ) {
214
	?>
215
				<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( $showmap , 1); ?> />
216
				<label for="<?php echo esc_attr( $this->get_field_id( 'showmap' ) ); ?>"><?php esc_html_e( 'Show map', 'jetpack' ); ?></label></p>
217
	<?php
218
			} else {
219
	?>
220
				<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></p>
221
				<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" />
222
	<?php
223
			}
224
	?>
225
226
				<p><label for="<?php echo esc_attr( $this->get_field_id( 'phone' ) ); ?>"><?php esc_html_e( 'Phone:', 'jetpack' ); ?></label>
227
				<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( $phone ); ?>" /></p>
228
229
				<p><label for="<?php echo esc_attr( $this->get_field_id( 'hours' ) ); ?>"><?php esc_html_e( 'Hours:', 'jetpack' ); ?></label>
230
231
				<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( $hours ); ?></textarea></p>
232
233
	<?php
234
		}
235
236
237
		function build_map_link( $address ) {
238
			// Google map urls have lots of available params but zoom (z) and query (q) are enough.
239
			return "http://maps.google.com/maps?z=16&q=" . $this->urlencode_address( $address );
240
		}
241
242
243
		function build_map( $lat, $lon ) {
244
245
			wp_enqueue_script( "jquery" );
246
			wp_enqueue_script( "google-maps", "https://maps.googleapis.com/maps/api/js?sensor=false" );
247
			wp_enqueue_script( "contact-info-map-js", plugins_url( 'contact-info/contact-info-map.js', __FILE__ ), array( 'jquery', 'google-maps' ), 20150127 );
248
			wp_enqueue_style( "contact-info-map-css", plugins_url( 'contact-info/contact-info-map.css', __FILE__ ), null, 20150127 );
249
250
			$lat = esc_attr( $lat );
251
			$lon = esc_attr( $lon );
252
			$html = <<<EOT
253
				<div class="contact-map">
254
				<input type="hidden" class="contact-info-map-lat" value="$lat" />
255
				<input type="hidden" class="contact-info-map-lon" value="$lon" />
256
				<div class="contact-info-map-canvas"></div></div>
257
EOT;
258
259
			return $html;
260
		}
261
262
263
		function urlencode_address( $address ) {
264
265
			$address = strtolower( $address );
266
			$address = preg_replace( "/\s+/", " ", trim( $address ) ); // Get rid of any unwanted whitespace
267
			$address = str_ireplace( " ", "+", $address ); // Use + not %20
268
			urlencode( $address );
269
270
			return $address;
271
		}
272
273
274
		function has_good_map( $instance ) {
275
			// The lat and lon of an address that could not be plotted will have values of 0 and 0.
276
			return ! ( $instance['lat'] == "0" && $instance['lon'] == "0" );
277
		}
278
279
	}
280
281
}
282