Completed
Push — fusion-sync/danroundhill/r2061... ( 6c7cf7...0f1caf )
by Jeremy
213:37 queued 206:26
created

modules/widgets/social-media-icons.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
Plugin Name: Social Media Icons Widget
4
Description: A simple widget that displays social media icons
5
Author: Automattic Inc.
6
7
This widget is now deprecated.
8
Any new features should go into modules/widgets/social-icons.php instead.
9
@see https://github.com/Automattic/jetpack/pull/8498
10
11
*/
12
13
14
/**
15
 * WPCOM_social_media_icons_widget class.
16
 *
17
 * @extends WP_Widget
18
 */
19
class WPCOM_social_media_icons_widget extends WP_Widget {
20
21
	/**
22
	 * Defaults
23
	 *
24
	 * @var mixed
25
	 * @access private
26
	 */
27
	private $defaults;
28
29
	/**
30
	 * Services
31
	 *
32
	 * @var mixed
33
	 * @access private
34
	 */
35
	private $services;
36
37
38
	/**
39
	 * __construct function.
40
	 *
41
	 * @access public
42
	 * @return void
43
	 */
44
	public function __construct() {
45
		parent::__construct(
46
			'wpcom_social_media_icons_widget',
47
			/** This filter is documented in modules/widgets/facebook-likebox.php */
48
			apply_filters( 'jetpack_widget_name', esc_html__( 'Social Media Icons (Deprecated)', 'jetpack' ) ),
49
			array(
50
				'description'                 => __( 'A simple widget that displays social media icons.', 'jetpack' ),
51
				'customize_selective_refresh' => true,
52
			)
53
		);
54
		$this->defaults = array(
55
			'title'               => __( 'Social', 'jetpack' ),
56
			'facebook_username'   => '',
57
			'twitter_username'    => '',
58
			'instagram_username'  => '',
59
			'pinterest_username'  => '',
60
			'linkedin_username'   => '',
61
			'github_username'     => '',
62
			'youtube_username'    => '',
63
			'vimeo_username'      => '',
64
			'googleplus_username' => '',
65
			'flickr_username'     => '',
66
			'wordpress_username'  => '',
67
			'twitch_username'     => '',
68
			'tumblr_username'     => '',
69
		);
70
		$this->services = array(
71
			'facebook'   => array( 'Facebook', 'https://www.facebook.com/%s/' ),
72
			'twitter'    => array( 'Twitter', 'https://twitter.com/%s/' ),
73
			'instagram'  => array( 'Instagram', 'https://www.instagram.com/%s/' ),
74
			'pinterest'  => array( 'Pinterest', 'https://www.pinterest.com/%s/' ),
75
			'linkedin'   => array( 'LinkedIn', 'https://www.linkedin.com/in/%s/' ),
76
			'github'     => array( 'GitHub', 'https://github.com/%s/' ),
77
			'youtube'    => array( 'YouTube', 'https://www.youtube.com/%s/' ),
78
			'vimeo'      => array( 'Vimeo', 'https://vimeo.com/%s/' ),
79
			'googleplus' => array( 'Google+', 'https://plus.google.com/u/0/%s/' ),
80
			'flickr'     => array( 'Flickr', 'https://www.flickr.com/photos/%s/' ),
81
			'wordpress'  => array( 'WordPress.org', 'https://profiles.wordpress.org/%s/' ),
82
			'twitch'     => array( 'Twitch', 'https://www.twitch.tv/%s/' ),
83
			'tumblr'     => array( 'Tumblr', 'https://%s.tumblr.com' ),
84
		);
85
		if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
86
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
87
		}
88
	}
89
90
	/**
91
	 * Enqueue Style.
92
	 *
93
	 * @access public
94
	 * @return void
95
	 */
96
	public function enqueue_style() {
97
		wp_register_style( 'jetpack_social_media_icons_widget', plugins_url( 'social-media-icons/style.css', __FILE__ ), array(), '20150602' );
98
		wp_enqueue_style( 'jetpack_social_media_icons_widget' );
99
	}
100
101
	/**
102
	 * Check Genericons.
103
	 *
104
	 * @access private
105
	 * @return Bool.
106
	 */
107
	private function check_genericons() {
108
		global $wp_styles;
109
		foreach ( $wp_styles->queue as $handle ) {
110
			if ( false !== stristr( $handle, 'genericons' ) ) {
111
				return $handle;
112
			}
113
		}
114
		return false;
115
	}
116
117
	/**
118
	 * Widget Front End.
119
	 *
120
	 * @access public
121
	 * @param mixed $args Arguments.
122
	 * @param mixed $instance Instance.
123
	 * @return void
124
	 */
125
	public function widget( $args, $instance ) {
126
		$instance = wp_parse_args( (array) $instance, $this->defaults );
127
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
128
		$instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
129
		if ( ! $this->check_genericons() ) {
130
			wp_enqueue_style( 'genericons' );
131
		}
132
		$index    = 10;
133
		$html     = array();
134
		$alt_text = esc_attr__( 'View %1$s&#8217;s profile on %2$s', 'jetpack' );
135
		foreach ( $this->services as $service => $data ) {
136
			list( $service_name, $url ) = $data;
137
			if ( ! isset( $instance[ $service . '_username' ] ) ) {
138
				continue;
139
			}
140
			$username = $link_username = $instance[ $service . '_username' ];
141
			if ( empty( $username ) ) {
142
				continue;
143
			}
144
			$index         += 10;
145
			$predefined_url = false;
146
147
			/** Check if full URL entered in configuration, use it instead of tinkering **/
148
			if (
149
				in_array(
150
					wp_parse_url( $username, PHP_URL_SCHEME ),
0 ignored issues
show
The call to wp_parse_url() has too many arguments starting with PHP_URL_SCHEME.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
151
					array( 'http', 'https' )
152
				)
153
			) {
154
				$predefined_url = $username;
155
156
				// In case of a predefined link we only display the service name
157
				// for screen readers
158
				$alt_text = '%2$s';
159
			}
160
161
			if ( 'googleplus' === $service
162
				&& ! is_numeric( $username )
163
				&& substr( $username, 0, 1 ) !== '+'
164
			) {
165
				$link_username = '+' . $username;
166
			}
167
			if ( 'youtube' === $service && 'UC' === substr( $username, 0, 2 ) ) {
168
				$link_username = 'channel/' . $username;
169
			} elseif ( 'youtube' === $service ) {
170
				$link_username = 'user/' . $username;
171
			}
172
173
			if ( ! $predefined_url ) {
174
				$predefined_url = sprintf( $url, $link_username );
175
			}
176
			/**
177
			 * Fires for each profile link in the social icons widget. Can be used
178
			 * to change the links for certain social networks if needed. All URLs
179
			 * will be passed through `esc_attr` on output.
180
			 *
181
			 * @module widgets
182
			 *
183
			 * @since 3.8.0
184
			 *
185
			 * @param string $url the currently processed URL
186
			 * @param string $service the lowercase service slug, e.g. 'facebook', 'youtube', etc.
187
			 */
188
			$link           = apply_filters(
189
				'jetpack_social_media_icons_widget_profile_link',
190
				$predefined_url,
191
				$service
192
			);
193
			$html[ $index ] = sprintf(
194
				'<a href="%1$s" class="genericon genericon-%2$s" target="_blank"><span class="screen-reader-text">%3$s</span></a>',
195
				esc_attr( $link ),
196
				esc_attr( $service ),
197
				sprintf( $alt_text, esc_html( $username ), $service_name )
198
			);
199
		}
200
		/**
201
		 * Fires at the end of the list of Social Media accounts.
202
		 * Can be used to add a new Social Media Site to the Social Media Icons Widget.
203
		 * The filter function passed the array of HTML entries that will be sorted
204
		 * by key, each wrapped in a list item element and output as an unsorted list.
205
		 *
206
		 * @module widgets
207
		 *
208
		 * @since 3.8.0
209
		 *
210
		 * @param array $html Associative array of HTML snippets per each icon.
211
		 */
212
		$html = apply_filters( 'jetpack_social_media_icons_widget_array', $html );
213
		ksort( $html );
214
		$html = '<ul><li>' . join( '</li><li>', $html ) . '</li></ul>';
215
		if ( ! empty( $instance['title'] ) ) {
216
			$html = $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] . $html;
217
		}
218
		$html = $args['before_widget'] . $html . $args['after_widget'];
219
220
		/** This action is documented in modules/widgets/gravatar-profile.php */
221
		do_action( 'jetpack_stats_extra', 'widget_view', 'social_media_icons' );
222
223
		/**
224
		 * Filters the Social Media Icons widget output.
225
		 *
226
		 * @module widgets
227
		 *
228
		 * @since 3.6.0
229
		 *
230
		 * @param string $html Social Media Icons widget html output.
231
		 */
232
		echo apply_filters( 'jetpack_social_media_icons_widget_output', $html );
233
	}
234
235
	/**
236
	 * Widget Settings.
237
	 *
238
	 * @access public
239
	 * @param mixed $instance Instance.
240
	 * @return void
241
	 */
242
	public function form( $instance ) {
243
		$instance = wp_parse_args( (array) $instance, $this->defaults );
244
		?>
245
			<p>
246
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'jetpack' ); ?></label>
247
				<input
248
						class="widefat"
249
						id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
250
						name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
251
						type="text"
252
						value="<?php echo esc_attr( $instance['title'] ); ?>"
253
					/>
254
			</p>
255
		<?php
256
		foreach ( $this->services as $service => $data ) {
257
			list( $service_name, $url ) = $data;
258
			?>
259
				<p>
260
					<label for="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>">
261
					<?php
262
						/* translators: %s is a social network name, e.g. Facebook. */
263
						printf( __( '%s username:', 'jetpack' ), $service_name );
264
					?>
265
				</label>
266
				<input
267
						class="widefat"
268
						id="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>"
269
						name="<?php echo esc_attr( $this->get_field_name( $service . '_username' ) ); ?>"
270
						type="text"
271
						value="<?php echo esc_attr( $instance[ $service . '_username' ] ); ?>"
272
					/>
273
				</p>
274
			<?php
275
		}
276
	}
277
278
	/**
279
	 * Update Widget Settings.
280
	 *
281
	 * @access public
282
	 * @param mixed $new_instance New Instance.
283
	 * @param mixed $old_instance Old Instance.
284
	 * @return Instance.
285
	 */
286
	public function update( $new_instance, $old_instance ) {
287
		$instance = (array) $old_instance;
288
		foreach ( $new_instance as $field => $value ) {
289
			$instance[ $field ] = sanitize_text_field( $new_instance[ $field ] );
290
		}
291
		// Stats.
292
		$stats = $instance;
293
		unset( $stats['title'] );
294
		$stats = array_filter( $stats );
295
		$stats = array_keys( $stats );
296
		$stats = array_map( array( $this, 'remove_username' ), $stats );
297
		foreach ( $stats as $val ) {
298
			/**
299
			 * Fires for each Social Media account being saved in the Social Media Widget settings.
300
			 *
301
			 * @module widgets
302
			 *
303
			 * @since 3.6.0
304
			 *
305
			 * @param string social-media-links-widget-svcs Type of action to track.
306
			 * @param string $val Name of the Social Media account being saved.
307
			 */
308
			do_action( 'jetpack_bump_stats_extras', 'social-media-links-widget-svcs', $val );
309
		}
310
		return $instance;
311
	}
312
313
	/**
314
	 * Remove username from value before to save stats.
315
	 *
316
	 * @access public
317
	 * @param mixed $val Value.
318
	 * @return Value.
319
	 */
320
	public function remove_username( $val ) {
321
		return str_replace( '_username', '', $val );
322
	}
323
} // End Class.
324
325
/**
326
 * Register and load the widget.
327
 *
328
 * @access public
329
 * @return void
330
 */
331
function wpcom_social_media_icons_widget_load_widget() {
332
	$transient  = 'wpcom_social_media_icons_widget::is_active';
333
	$has_widget = get_transient( $transient );
334
335
	if ( false === $has_widget ) {
336
		$is_active_widget = is_active_widget( false, false, 'wpcom_social_media_icons_widget', false );
337
		$has_widget       = (int) ! empty( $is_active_widget );
338
		set_transient( $transient, $has_widget, 1 * HOUR_IN_SECONDS );
339
	}
340
341
	// [DEPRECATION]: Only register widget if active widget exists already
342
	if ( $has_widget ) {
343
		register_widget( 'wpcom_social_media_icons_widget' );
344
	}
345
}
346
add_action( 'widgets_init', 'wpcom_social_media_icons_widget_load_widget' );
347