Completed
Push — fix/issue-5159 ( e5f9c0...935047 )
by
unknown
55:37 queued 42:38
created

WPCOM_social_media_icons_widget::enqueue_style()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
Plugin Name: Social Media Icons Widget
4
Description: A simple widget that displays social media icons
5
Author: Chris Rudzki
6
*/
7
8
9
/**
10
 * WPCOM_social_media_icons_widget class.
11
 *
12
 * @extends WP_Widget
13
 */
14
class WPCOM_social_media_icons_widget extends WP_Widget {
15
16
	/**
17
	 * Defaults
18
	 *
19
	 * @var mixed
20
	 * @access private
21
	 */
22
	private $defaults;
23
24
	/**
25
	 * Services
26
	 *
27
	 * @var mixed
28
	 * @access private
29
	 */
30
	private $services;
31
32
33
	/**
34
	 * __construct function.
35
	 *
36
	 * @access public
37
	 * @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...
38
	 */
39
	public function __construct() {
40
		parent::__construct(
41
			'wpcom_social_media_icons_widget',
42
			/** This filter is documented in modules/widgets/facebook-likebox.php */
43
			apply_filters( 'jetpack_widget_name', esc_html__( 'Social Media Icons', 'jetpack' ) ),
44
			array(
45
				'description' => __( 'A simple widget that displays social media icons.', 'jetpack' ),
46
				'customize_selective_refresh' => true,
47
			)
48
		);
49
		$this->defaults = array(
50
			'title'               => __( 'Social', 'jetpack' ),
51
			'facebook_username'   => '',
52
			'twitter_username'    => '',
53
			'instagram_username'  => '',
54
			'pinterest_username'  => '',
55
			'linkedin_username'   => '',
56
			'github_username'     => '',
57
			'youtube_username'    => '',
58
			'vimeo_username'      => '',
59
			'googleplus_username' => '',
60
			'flickr_username'     => '',
61
			'wordpress_username'  => '',
62
		);
63
		$this->services = array(
64
			'facebook'   => array( 'Facebook', 'https://www.facebook.com/%s/' ),
65
			'twitter'    => array( 'Twitter', 'https://twitter.com/%s/' ),
66
			'instagram'  => array( 'Instagram', 'https://instagram.com/%s/' ),
67
			'pinterest'  => array( 'Pinterest', 'https://www.pinterest.com/%s/' ),
68
			'linkedin'   => array( 'LinkedIn', 'https://www.linkedin.com/in/%s/' ),
69
			'github'     => array( 'GitHub', 'https://github.com/%s/' ),
70
			'youtube'    => array( 'YouTube', 'https://www.youtube.com/%s/' ),
71
			'vimeo'      => array( 'Vimeo', 'https://vimeo.com/%s/' ),
72
			'googleplus' => array( 'Google+', 'https://plus.google.com/u/0/%s/' ),
73
			'flickr'     => array( 'Flickr', 'https://www.flickr.com/photos/%s/' ),
74
			'wordpress'  => array( 'WordPress.org', 'https://profiles.wordpress.org/%s/' ),
75
		);
76 View Code Duplication
		if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
77
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
78
		}
79
	}
80
81
	/**
82
	 * Enqueue Style.
83
	 *
84
	 * @access public
85
	 * @return void
86
	 */
87
	public function enqueue_style() {
88
		wp_register_style( 'jetpack_social_media_icons_widget', plugins_url( 'social-media-icons/style.css', __FILE__ ), array(), '20150602' );
89
		wp_enqueue_style( 'jetpack_social_media_icons_widget' );
90
	}
91
92
	/**
93
	 * Check Genericons.
94
	 *
95
	 * @access private
96
	 * @return Bool.
0 ignored issues
show
Documentation introduced by
The doc-type Bool. could not be parsed: Unknown type name "Bool." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
97
	 */
98
	private function check_genericons() {
99
		global $wp_styles;
100
		foreach ( $wp_styles->queue as $handle ) {
101
			if ( false !== stristr( $handle, 'genericons' ) ) {
102
				return $handle;
103
			}
104
		}
105
		return false;
106
	}
107
108
	/**
109
	 * Widget Front End.
110
	 *
111
	 * @access public
112
	 * @param mixed $args Arguments.
113
	 * @param mixed $instance Instance.
114
	 * @return void
115
	 */
116
	public function widget( $args, $instance ) {
117
		$instance = wp_parse_args( (array) $instance, $this->defaults );
118
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
119
		$instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
120
		if ( ! $this->check_genericons() ) {
121
			wp_enqueue_style( 'genericons' );
122
		}
123
		$index = 10;
124
		$html = array();
125
		$alt_text = esc_attr__( 'View %1$s&#8217;s profile on %2$s', 'jetpack' );
126
		foreach ( $this->services as $service => $data ) {
127
			list( $service_name, $url ) = $data;
128
			if ( ! isset( $instance[ $service . '_username' ] ) ) {
129
				continue;
130
			}
131
			$username = $link_username = $instance[ $service . '_username' ];
132
			if ( empty( $username ) ) {
133
				continue;
134
			}
135
			$index += 10;
136
			if (
137
				'googleplus' === $service
138
				&& ! is_numeric( $username )
139
				&& substr( $username, 0, 1 ) !== '+'
140
			) {
141
				$link_username = '+' . $username;
142
			}
143
			if ( 'youtube' === $service && 'UC' === substr( $username, 0, 2 ) ) {
144
				$link_username = 'channel/' . $username;
145
			} else if ( 'youtube' === $service ) {
146
				$link_username = 'user/' . $username;
147
			}
148
			/**
149
			 * Fires for each profile link in the social icons widget. Can be used
150
			 * to change the links for certain social networks if needed.
151
			 *
152
			 * @module widgets
153
			 *
154
			 * @since 3.8.0
155
			 *
156
			 * @param string $url the currently processed URL
157
			 * @param string $service the lowercase service slug, e.g. 'facebook', 'youtube', etc.
158
			 */
159
			$link = apply_filters( 'jetpack_social_media_icons_widget_profile_link', esc_url( sprintf( $url, $link_username ) ), $service );
160
			$html[ $index ] =
161
				'<a href="' . $link
162
				. '" class="genericon genericon-' . $service . '" target="_blank"><span class="screen-reader-text">'
163
				. sprintf( $alt_text, esc_html( $username ), $service_name )
164
				. '</span></a>';
165
		}
166
		/**
167
		 * Fires at the end of the list of Social Media accounts.
168
		 * Can be used to add a new Social Media Site to the Social Media Icons Widget.
169
		 * The filter function passed the array of HTML entries that will be sorted
170
		 * by key, each wrapped in a list item element and output as an unsorted list.
171
		 *
172
		 * @module widgets
173
		 *
174
		 * @since 3.8.0
175
		 *
176
		 * @param array $html Associative array of HTML snippets per each icon.
177
		 */
178
		$html = apply_filters( 'jetpack_social_media_icons_widget_array', $html );
179
		ksort( $html );
180
		$html = '<ul><li>' . join( '</li><li>', $html ) . '</li></ul>';
181 View Code Duplication
		if ( ! empty( $instance['title'] ) ) {
182
			$html = $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] . $html;
183
		}
184
		$html = $args['before_widget'] . $html . $args['after_widget'];
185
		/**
186
		 * Filters the Social Media Icons widget output.
187
		 *
188
		 * @module widgets
189
		 *
190
		 * @since 3.6.0
191
		 *
192
		 * @param string $html Social Media Icons widget html output.
193
		 */
194
		echo apply_filters( 'jetpack_social_media_icons_widget_output', $html );
195
	}
196
197
	/**
198
	 * Widget Settings.
199
	 *
200
	 * @access public
201
	 * @param mixed $instance Instance.
202
	 * @return void
203
	 */
204
	public function form( $instance ) {
205
		$instance = wp_parse_args( (array) $instance, $this->defaults );
206
		?>
207
			<p>
208
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'jetpack' ); ?></label>
209
				<input
210
						class="widefat"
211
						id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
212
						name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
213
						type="text"
214
						value="<?php echo esc_attr( $instance['title'] ); ?>"
215
					/>
216
			</p>
217
		<?php
218
		foreach ( $this->services as $service => $data ) {
219
			list( $service_name, $url ) = $data;
0 ignored issues
show
Unused Code introduced by
The assignment to $url is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
220
			?>
221
				<p>
222
					<label for="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>">
223
					<?php
224
						/* Translators: %s is a social network name, e.g. Facebook. */
225
						printf( __( '%s username:', 'jetpack' ), $service_name );
226
					?>
227
				</label>
228
				<input
229
						class="widefat"
230
						id="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>"
231
						name="<?php echo esc_attr( $this->get_field_name( $service . '_username' ) ); ?>"
232
						type="text"
233
						value="<?php echo esc_attr( $instance[ $service . '_username' ] ); ?>"
234
					/>
235
				</p>
236
			<?php
237
		}
238
	}
239
240
	/**
241
	 * Update Widget Settings.
242
	 *
243
	 * @access public
244
	 * @param mixed $new_instance New Instance.
245
	 * @param mixed $old_instance Old Instance.
246
	 * @return Instance.
0 ignored issues
show
Documentation introduced by
The doc-type Instance. could not be parsed: Unknown type name "Instance." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
247
	 */
248
	public function update( $new_instance, $old_instance ) {
249
		$instance = (array) $old_instance;
250
		foreach ( $new_instance as $field => $value ) {
251
			$instance[ $field ] = sanitize_text_field( $new_instance[ $field ] );
252
		}
253
		// Stats.
254
		$stats = $instance;
255
		unset( $stats['title'] );
256
		$stats = array_filter( $stats );
257
		$stats = array_keys( $stats );
258
		$stats = array_map( array( $this, 'remove_username' ), $stats );
259
		foreach ( $stats as $val ) {
260
			/**
261
			 * Fires for each Social Media account being saved in the Social Media Widget settings.
262
			 *
263
			 * @module widgets
264
			 *
265
			 * @since 3.6.0
266
			 *
267
			 * @param string social-media-links-widget-svcs Type of action to track.
268
			 * @param string $val Name of the Social Media account being saved.
269
			 */
270
			do_action( 'jetpack_bump_stats_extras', 'social-media-links-widget-svcs', $val );
271
		}
272
		return $instance;
273
	}
274
275
	/**
276
	 * Remove username from value before to save stats.
277
	 *
278
	 * @access public
279
	 * @param mixed $val Value.
280
	 * @return Value.
0 ignored issues
show
Documentation introduced by
The doc-type Value. could not be parsed: Unknown type name "Value." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
281
	 */
282
	public function remove_username( $val ) {
283
		return str_replace( '_username', '', $val );
284
	}
285
} // End Class.
286
287
/**
288
 * Register and load the widget.
289
 *
290
 * @access public
291
 * @return void
292
 */
293
function wpcom_social_media_icons_widget_load_widget() {
294
	register_widget( 'wpcom_social_media_icons_widget' );
295
}
296
add_action( 'widgets_init', 'wpcom_social_media_icons_widget_load_widget' );
297