Completed
Push — remove/genericons ( db1b4a...c6fc39 )
by
unknown
295:37 queued 286:36
created

check_social_icons()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
rs 9.4285
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
// Creating the widget
9
10
class WPCOM_social_media_icons_widget extends WP_Widget {
11
12
	private $defaults;
13
14
	private $services;
15
16
	public function __construct() {
17
		parent::__construct(
18
			'wpcom_social_media_icons_widget',
19
			/** This filter is documented in modules/widgets/facebook-likebox.php */
20
			apply_filters( 'jetpack_widget_name', esc_html__( 'Social Media Icons', 'jetpack' ) ),
21
			array(
22
				'description' => __( 'A simple widget that displays social media icons.', 'jetpack' ),
23
				'customize_selective_refresh' => true,
24
			)
25
		);
26
27
		$this->defaults = array(
28
			'title'              => __( 'Social', 'jetpack' ),
29
			'facebook_username'  => '',
30
			'twitter_username'   => '',
31
			'instagram_username' => '',
32
			'pinterest_username' => '',
33
			'linkedin_username'  => '',
34
			'github_username'    => '',
35
			'youtube_username'   => '',
36
			'vimeo_username'     => '',
37
			'googleplus_username' => '',
38
		);
39
40
		$this->services = array(
41
			'facebook'   => array( 'Facebook', 'https://www.facebook.com/%s/' ),
42
			'twitter'    => array( 'Twitter', 'https://twitter.com/%s/' ),
43
			'instagram'  => array( 'Instagram', 'https://instagram.com/%s/' ),
44
			'pinterest'  => array( 'Pinterest', 'https://www.pinterest.com/%s/' ),
45
			'linkedin'   => array( 'LinkedIn', 'https://www.linkedin.com/in/%s/' ),
46
			'github'     => array( 'GitHub', 'https://github.com/%s/' ),
47
			'youtube'    => array( 'YouTube', 'https://www.youtube.com/%s/' ),
48
			'vimeo'      => array( 'Vimeo', 'https://vimeo.com/%s/' ),
49
			'googleplus' => array( 'Google+', 'https://plus.google.com/u/0/%s/' ),
50
		);
51
52 View Code Duplication
		if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
53
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
54
		}
55
	}
56
57
	public function enqueue_style() {
58
		wp_register_style( 'jetpack_social_media_icons_widget', plugins_url( 'social-media-icons/style.css', __FILE__ ), array(), '20150602' );
59
		wp_enqueue_style( 'jetpack_social_media_icons_widget' );
60
	}
61
62
	private function check_social_icons() {
63
		global $wp_styles;
64
65
		foreach ( $wp_styles->queue as $handle ) {
66
			if ( false !== stristr( $handle, 'social-logos' ) ) {
67
				return $handle;
68
			}
69
		}
70
71
		return false;
72
	}
73
74
	// front end
75
	public function widget( $args, $instance ) {
76
		$instance = wp_parse_args( (array) $instance, $this->defaults );
77
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
78
		$instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
79
80
		if ( ! $this->check_social_icons() ) {
81
			wp_enqueue_style( 'social-logos' );
82
		}
83
84
		$index = 10;
85
		$html = array();
86
87
		$alt_text = esc_attr__( 'View %1$s&#8217;s profile on %2$s', 'jetpack' );
88
89
		foreach ( $this->services as $service => $data ) {
90
			list( $service_name, $url ) = $data;
91
92
			if ( ! isset( $instance[ $service . '_username' ] ) ) {
93
				continue;
94
			}
95
			$username = $link_username = $instance[ $service . '_username' ];
96
97
			if ( empty( $username ) ) {
98
				continue;
99
			}
100
101
			$index += 10;
102
103
			if (
104
				$service === 'googleplus'
105
				&& ! is_numeric( $username )
106
				&& substr( $username, 0, 1 ) !== "+"
107
			) {
108
				$link_username = "+" . $username;
109
			}
110
111
			if ( $service === 'youtube' && substr( $username, 0, 2 ) == 'UC' ) {
112
				$link_username = "channel/" . $username;
113
			} else if ( $service === 'youtube' ) {
114
				$link_username = "user/" . $username;
115
			}
116
117
			/**
118
			 * Fires for each profile link in the social icons widget. Can be used
119
			 * to change the links for certain social networks if needed.
120
			 *
121
			 * @module widgets
122
			 *
123
			 * @since 3.8.0
124
			 *
125
			 * @param string $url the currently processed URL
126
			 * @param string $service the lowercase service slug, e.g. 'facebook', 'youtube', etc.
127
			 */
128
			$link = apply_filters( 'jetpack_social_media_icons_widget_profile_link', esc_url( sprintf( $url, $link_username ) ), $service );
129
130
			$html[ $index ] =
131
				'<a title="' . sprintf( $alt_text, esc_attr( $username ), $service_name )
132
				. '" href="' . $link
133
				. '" class="genericon genericon-' . $service . '" target="_blank"><span class="screen-reader-text">'
134
				. sprintf( $alt_text, esc_html( $username ), $service_name )
135
				. '</span></a>';
136
		}
137
138
		/**
139
		 * Fires at the end of the list of Social Media accounts.
140
		 * Can be used to add a new Social Media Site to the Social Media Icons Widget.
141
		 * The filter function passed the array of HTML entries that will be sorted
142
		 * by key, each wrapped in a list item element and output as an unsorted list.
143
		 *
144
		 * @module widgets
145
		 *
146
		 * @since 3.8.0
147
		 *
148
		 * @param array $html Associative array of HTML snippets per each icon.
149
		 */
150
		$html = apply_filters( 'jetpack_social_media_icons_widget_array', $html );
151
152
		ksort( $html );
153
		$html = '<ul><li>' . join( '</li><li>', $html ) . '</li></ul>';
154
155 View Code Duplication
		if ( ! empty( $instance['title'] ) ) {
156
			$html = $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] . $html;
157
		}
158
159
		$html = $args['before_widget'] . $html . $args['after_widget'];
160
161
		/**
162
		 * Filters the Social Media Icons widget output.
163
		 *
164
		 * @module widgets
165
		 *
166
		 * @since 3.6.0
167
		 *
168
		 * @param string $html Social Media Icons widget html output.
169
		 */
170
		echo apply_filters( 'jetpack_social_media_icons_widget_output', $html );
171
	}
172
173
	// back end
174
	public function form( $instance ) {
175
		$instance = wp_parse_args( (array) $instance, $this->defaults );
176
		?>
177
			<p>
178
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'jetpack' ); ?></label>
179
				<input
180
						class="widefat"
181
						id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
182
						name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
183
						type="text"
184
						value="<?php echo esc_attr( $instance['title'] ); ?>"
185
					/>
186
			</p>
187
		<?php
188
189
		foreach ( $this->services as $service => $data ) {
190
			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...
191
			?>
192
				<p>
193
					<label for="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>">
194
					<?php
195
						/* translators: %s is a social network name, e.g. Facebook */
196
						printf( __( '%s username:', 'jetpack' ), $service_name );
197
					?>
198
				</label>
199
				<input
200
						class="widefat"
201
						id="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>"
202
						name="<?php echo esc_attr( $this->get_field_name( $service . '_username' ) ); ?>"
203
						type="text"
204
						value="<?php echo esc_attr( $instance[ $service . '_username'] ); ?>"
205
					/>
206
				</p>
207
			<?php
208
		}
209
	}
210
211
	// updating widget settings
212
	public function update( $new_instance, $old_instance ) {
213
		$instance = (array) $old_instance;
214
215
		foreach ( $new_instance as $field => $value ) {
216
			$instance[$field] = sanitize_text_field( $new_instance[$field] );
217
		}
218
219
		// Stats
220
		$stats = $instance;
221
		unset( $stats['title'] );
222
		$stats = array_filter( $stats );
223
		$stats = array_keys( $stats );
224
		$stats = array_map( array( $this, 'remove_username' ), $stats );
225
		foreach ( $stats as $val ) {
226
			/**
227
			 * Fires for each Social Media account being saved in the Social Media Widget settings.
228
			 *
229
			 * @module widgets
230
			 *
231
			 * @since 3.6.0
232
			 *
233
			 * @param string social-media-links-widget-svcs Type of action to track.
234
			 * @param string $val Name of the Social Media account being saved.
235
			 */
236
			do_action( 'jetpack_bump_stats_extras', 'social-media-links-widget-svcs', $val ) ;
237
		}
238
239
		return $instance;
240
	}
241
242
	// Remove username from value before to save stats
243
	public function remove_username( $val ) {
244
		return str_replace( '_username', '', $val );
245
	}
246
247
} // class ends here
248
249
// register and load the widget
250
function wpcom_social_media_icons_widget_load_widget() {
251
	register_widget( 'wpcom_social_media_icons_widget' );
252
}
253
add_action( 'widgets_init', 'wpcom_social_media_icons_widget_load_widget' );
254