Completed
Push — master-stable ( c165f1...74e410 )
by
unknown
08:22
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
		);
62
		$this->services = array(
63
			'facebook'   => array( 'Facebook', 'https://www.facebook.com/%s/' ),
64
			'twitter'    => array( 'Twitter', 'https://twitter.com/%s/' ),
65
			'instagram'  => array( 'Instagram', 'https://instagram.com/%s/' ),
66
			'pinterest'  => array( 'Pinterest', 'https://www.pinterest.com/%s/' ),
67
			'linkedin'   => array( 'LinkedIn', 'https://www.linkedin.com/in/%s/' ),
68
			'github'     => array( 'GitHub', 'https://github.com/%s/' ),
69
			'youtube'    => array( 'YouTube', 'https://www.youtube.com/%s/' ),
70
			'vimeo'      => array( 'Vimeo', 'https://vimeo.com/%s/' ),
71
			'googleplus' => array( 'Google+', 'https://plus.google.com/u/0/%s/' ),
72
			'flickr'     => array( 'Flickr', 'https://www.flickr.com/photos/%s/' ),
73
		);
74 View Code Duplication
		if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
75
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
76
		}
77
	}
78
79
	/**
80
	 * Enqueue Style.
81
	 *
82
	 * @access public
83
	 * @return void
84
	 */
85
	public function enqueue_style() {
86
		wp_register_style( 'jetpack_social_media_icons_widget', plugins_url( 'social-media-icons/style.css', __FILE__ ), array(), '20150602' );
87
		wp_enqueue_style( 'jetpack_social_media_icons_widget' );
88
	}
89
90
	/**
91
	 * Check Genericons.
92
	 *
93
	 * @access private
94
	 * @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...
95
	 */
96
	private function check_genericons() {
97
		global $wp_styles;
98
		foreach ( $wp_styles->queue as $handle ) {
99
			if ( false !== stristr( $handle, 'genericons' ) ) {
100
				return $handle;
101
			}
102
		}
103
		return false;
104
	}
105
106
	/**
107
	 * Widget Front End.
108
	 *
109
	 * @access public
110
	 * @param mixed $args Arguments.
111
	 * @param mixed $instance Instance.
112
	 * @return void
113
	 */
114
	public function widget( $args, $instance ) {
115
		$instance = wp_parse_args( (array) $instance, $this->defaults );
116
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
117
		$instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
118
		if ( ! $this->check_genericons() ) {
119
			wp_enqueue_style( 'genericons' );
120
		}
121
		$index = 10;
122
		$html = array();
123
		$alt_text = esc_attr__( 'View %1$s&#8217;s profile on %2$s', 'jetpack' );
124
		foreach ( $this->services as $service => $data ) {
125
			list( $service_name, $url ) = $data;
126
			if ( ! isset( $instance[ $service . '_username' ] ) ) {
127
				continue;
128
			}
129
			$username = $link_username = $instance[ $service . '_username' ];
130
			if ( empty( $username ) ) {
131
				continue;
132
			}
133
			$index += 10;
134
			if (
135
				'googleplus' === $service
136
				&& ! is_numeric( $username )
137
				&& substr( $username, 0, 1 ) !== '+'
138
			) {
139
				$link_username = '+' . $username;
140
			}
141
			if ( 'youtube' === $service && 'UC' === substr( $username, 0, 2 ) ) {
142
				$link_username = 'channel/' . $username;
143
			} else if ( 'youtube' === $service ) {
144
				$link_username = 'user/' . $username;
145
			}
146
			/**
147
			 * Fires for each profile link in the social icons widget. Can be used
148
			 * to change the links for certain social networks if needed.
149
			 *
150
			 * @module widgets
151
			 *
152
			 * @since 3.8.0
153
			 *
154
			 * @param string $url the currently processed URL
155
			 * @param string $service the lowercase service slug, e.g. 'facebook', 'youtube', etc.
156
			 */
157
			$link = apply_filters( 'jetpack_social_media_icons_widget_profile_link', esc_url( sprintf( $url, $link_username ) ), $service );
158
			$html[ $index ] =
159
				'<a title="' . sprintf( $alt_text, esc_attr( $username ), $service_name )
160
				. '" href="' . $link
161
				. '" class="genericon genericon-' . $service . '" target="_blank"><span class="screen-reader-text">'
162
				. sprintf( $alt_text, esc_html( $username ), $service_name )
163
				. '</span></a>';
164
		}
165
		/**
166
		 * Fires at the end of the list of Social Media accounts.
167
		 * Can be used to add a new Social Media Site to the Social Media Icons Widget.
168
		 * The filter function passed the array of HTML entries that will be sorted
169
		 * by key, each wrapped in a list item element and output as an unsorted list.
170
		 *
171
		 * @module widgets
172
		 *
173
		 * @since 3.8.0
174
		 *
175
		 * @param array $html Associative array of HTML snippets per each icon.
176
		 */
177
		$html = apply_filters( 'jetpack_social_media_icons_widget_array', $html );
178
		ksort( $html );
179
		$html = '<ul><li>' . join( '</li><li>', $html ) . '</li></ul>';
180 View Code Duplication
		if ( ! empty( $instance['title'] ) ) {
181
			$html = $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] . $html;
182
		}
183
		$html = $args['before_widget'] . $html . $args['after_widget'];
184
		/**
185
		 * Filters the Social Media Icons widget output.
186
		 *
187
		 * @module widgets
188
		 *
189
		 * @since 3.6.0
190
		 *
191
		 * @param string $html Social Media Icons widget html output.
192
		 */
193
		echo apply_filters( 'jetpack_social_media_icons_widget_output', $html );
194
	}
195
196
	/**
197
	 * Widget Settings.
198
	 *
199
	 * @access public
200
	 * @param mixed $instance Instance.
201
	 * @return void
202
	 */
203
	public function form( $instance ) {
204
		$instance = wp_parse_args( (array) $instance, $this->defaults );
205
		?>
206
			<p>
207
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'jetpack' ); ?></label>
208
				<input
209
						class="widefat"
210
						id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
211
						name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
212
						type="text"
213
						value="<?php echo esc_attr( $instance['title'] ); ?>"
214
					/>
215
			</p>
216
		<?php
217
		foreach ( $this->services as $service => $data ) {
218
			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...
219
			?>
220
				<p>
221
					<label for="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>">
222
					<?php
223
						/* Translators: %s is a social network name, e.g. Facebook. */
224
						printf( __( '%s username:', 'jetpack' ), $service_name );
225
					?>
226
				</label>
227
				<input
228
						class="widefat"
229
						id="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>"
230
						name="<?php echo esc_attr( $this->get_field_name( $service . '_username' ) ); ?>"
231
						type="text"
232
						value="<?php echo esc_attr( $instance[ $service . '_username' ] ); ?>"
233
					/>
234
				</p>
235
			<?php
236
		}
237
	}
238
239
	/**
240
	 * Update Widget Settings.
241
	 *
242
	 * @access public
243
	 * @param mixed $new_instance New Instance.
244
	 * @param mixed $old_instance Old Instance.
245
	 * @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...
246
	 */
247
	public function update( $new_instance, $old_instance ) {
248
		$instance = (array) $old_instance;
249
		foreach ( $new_instance as $field => $value ) {
250
			$instance[ $field ] = sanitize_text_field( $new_instance[ $field ] );
251
		}
252
		// Stats.
253
		$stats = $instance;
254
		unset( $stats['title'] );
255
		$stats = array_filter( $stats );
256
		$stats = array_keys( $stats );
257
		$stats = array_map( array( $this, 'remove_username' ), $stats );
258
		foreach ( $stats as $val ) {
259
			/**
260
			 * Fires for each Social Media account being saved in the Social Media Widget settings.
261
			 *
262
			 * @module widgets
263
			 *
264
			 * @since 3.6.0
265
			 *
266
			 * @param string social-media-links-widget-svcs Type of action to track.
267
			 * @param string $val Name of the Social Media account being saved.
268
			 */
269
			do_action( 'jetpack_bump_stats_extras', 'social-media-links-widget-svcs', $val );
270
		}
271
		return $instance;
272
	}
273
274
	/**
275
	 * Remove username from value before to save stats.
276
	 *
277
	 * @access public
278
	 * @param mixed $val Value.
279
	 * @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...
280
	 */
281
	public function remove_username( $val ) {
282
		return str_replace( '_username', '', $val );
283
	}
284
} // End Class.
285
286
/**
287
 * Register and load the widget.
288
 *
289
 * @access public
290
 * @return void
291
 */
292
function wpcom_social_media_icons_widget_load_widget() {
293
	register_widget( 'wpcom_social_media_icons_widget' );
294
}
295
add_action( 'widgets_init', 'wpcom_social_media_icons_widget_load_widget' );
296