Completed
Push — add/image-block-detection ( 198131...be1940 )
by
unknown
06:25
created

Jetpack_Widget_Social_Icons::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
class Jetpack_Widget_Social_Icons extends WP_Widget {
3
	/**
4
	 * @var array Default widget options.
5
	 */
6
	protected $defaults;
7
8
	/**
9
	 * Widget constructor.
10
	 */
11
	public function __construct() {
12
		$widget_ops = array(
13
			'classname'                   => 'jetpack_widget_social_icons',
14
			'description'                 => __( 'Add social-media icons to your site.', 'jetpack' ),
15
			'customize_selective_refresh' => true,
16
		);
17
18
		parent::__construct(
19
			'jetpack_widget_social_icons',
20
			/** This filter is documented in modules/widgets/facebook-likebox.php */
21
			apply_filters( 'jetpack_widget_name', __( 'Social Icons', 'jetpack' ) ),
22
			$widget_ops
23
		);
24
25
		$this->defaults = array(
26
			'title'     => __( 'Follow Us', 'jetpack' ),
27
			'icon-size' => 'medium',
28
			'new-tab'   => false,
29
			'icons'     => array(
30
				array(
31
					'url' => '',
32
				),
33
			),
34
		);
35
36
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
37
		add_action( 'admin_print_footer_scripts', array( $this, 'render_admin_js' ) );
38
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_icon_scripts' ) );
39
		add_action( 'wp_footer', array( $this, 'include_svg_icons' ), 9999 );
40
	}
41
42
	/**
43
	 * Script & styles for admin widget form.
44
	 */
45
	public function enqueue_admin_scripts() {
46
		wp_enqueue_script( 'jetpack-widget-social-icons-script', plugins_url( 'social-icons/social-icons-admin.js', __FILE__ ), array( 'jquery-ui-sortable' ), '20170506' );
47
		wp_enqueue_style( 'jetpack-widget-social-icons-admin', plugins_url( 'social-icons/social-icons-admin.css', __FILE__ ), array(), '20170506' );
48
	}
49
50
	/**
51
	 * Styles for front-end widget.
52
	 */
53
	public function enqueue_icon_scripts() {
54
		wp_enqueue_style( 'jetpack-widget-social-icons-styles', plugins_url( 'social-icons/social-icons.css', __FILE__ ), array(), '20170506' );
55
	}
56
57
	/**
58
	 * JavaScript for admin widget form.
59
	 */
60
	public function render_admin_js() {
61
		global $wp_customize;
62
		global $pagenow;
63
64
		if ( ! isset( $wp_customize ) && 'widgets.php' !== $pagenow ) {
65
			return;
66
		}
67
	?>
68
		<script type="text/html" id="tmpl-jetpack-widget-social-icons-template">
69
			<?php self::render_icons_template(); ?>
70
		</script>
71
	<?php
72
	}
73
74
	/**
75
	 * Add SVG definitions to the footer.
76
	 */
77
	public function include_svg_icons() {
78
		if ( ! is_active_widget( false, $this->id, $this->id_base, true ) ) {
79
			return;
80
		}
81
82
		// Define SVG sprite file in Jetpack
83
		$svg_icons = dirname( dirname( __FILE__ ) ) . '/theme-tools/social-menu/social-menu.svg';
84
85
		// Define SVG sprite file in WPCOM
86
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
87
			$svg_icons = dirname( dirname( __FILE__ ) ) . '/social-menu/social-menu.svg';
88
		}
89
90
		// If it exists, include it.
91
		if ( is_file( $svg_icons ) ) {
92
			require_once( $svg_icons );
93
		}
94
	}
95
96
	/**
97
	 * Front-end display of widget.
98
	 *
99
	 * @see WP_Widget::widget()
100
	 *
101
	 * @param array $args Widget arguments.
102
	 * @param array $instance Saved values from database.
103
	 */
104
	public function widget( $args, $instance ) {
105
		$instance = wp_parse_args( $instance, $this->defaults );
106
107
		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
108
		$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
109
110
		echo $args['before_widget'];
111
112
		if ( ! empty( $title ) ) {
113
			echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
114
		}
115
116
		if ( ! empty( $instance['icons'] ) ) :
117
118
			// Get supported social icons.
119
			$social_icons = $this->get_supported_icons();
120
			$default_icon = $this->get_svg_icon( array( 'icon' => 'chain' ) );
121
122
			// Set target attribute for the link
123
			if ( true === $instance['new-tab'] ) {
124
				$target = '_blank';
125
			} else {
126
				$target = '_self';
127
			}
128
		?>
129
130
			<ul class="jetpack-social-widget-list size-<?php echo esc_attr( $instance['icon-size'] ); ?>">
131
132
				<?php foreach ( $instance['icons'] as $icon ) : ?>
133
134
					<?php if ( ! empty( $icon['url'] ) ) : ?>
135
						<li class="jetpack-social-widget-item">
136
							<a href="<?php echo esc_url( $icon['url'], array( 'http', 'https', 'mailto', 'skype' ) ); ?>" target="<?php echo $target; ?>">
137
								<?php
138
									$found_icon = false;
139
140
								foreach ( $social_icons as $social_icon ) {
141
									if ( false !== stripos( $icon['url'], $social_icon['url'] ) ) {
142
										echo '<span class="screen-reader-text">' . esc_attr( $social_icon['label'] ) . '</span>';
143
										echo $this->get_svg_icon( array( 'icon' => esc_attr( $social_icon['icon'] ) ) );
144
										$found_icon = true;
145
										break;
146
									}
147
								}
148
149
								if ( ! $found_icon ) {
150
									echo $default_icon;
151
								}
152
								?>
153
							</a>
154
						</li>
155
					<?php endif; ?>
156
157
				<?php endforeach; ?>
158
159
			</ul>
160
161
		<?php
162
		endif;
163
164
		echo $args['after_widget'];
165
166
		/** This action is documented in modules/widgets/gravatar-profile.php */
167
		do_action( 'jetpack_stats_extra', 'widget_view', 'social_icons' );
168
	}
169
170
	/**
171
	 * Sanitize widget form values as they are saved.
172
	 *
173
	 * @see WP_Widget::update()
174
	 *
175
	 * @param array $new_instance Values just sent to be saved.
176
	 * @param array $old_instance Previously saved values from database.
177
	 *
178
	 * @return array Updated safe values to be saved.
179
	 */
180
	public function update( $new_instance, $old_instance ) {
181
		$instance['title']     = sanitize_text_field( $new_instance['title'] );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$instance was never initialized. Although not strictly required by PHP, it is generally a good practice to add $instance = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
182
		$instance['icon-size'] = $this->defaults['icon-size'];
183
184
		if ( in_array( $new_instance['icon-size'], array( 'small', 'medium', 'large' ) ) ) {
185
			$instance['icon-size'] = $new_instance['icon-size'];
186
		}
187
188
		$instance['new-tab'] = isset( $new_instance['new-tab'] ) ? (bool) $new_instance['new-tab'] : false;
189
		$icon_count          = count( $new_instance['url-icons'] );
0 ignored issues
show
Unused Code introduced by
$icon_count 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...
190
		$instance['icons']   = array();
191
192
		foreach ( $new_instance['url-icons'] as $url ) {
193
			$url = filter_var( $url, FILTER_SANITIZE_URL );
194
195
			if ( ! empty( $url ) ) {
196
				$instance['icons'][] = array(
197
					'url' => $url,
198
				);
199
			}
200
		}
201
202
		return $instance;
203
	}
204
205
	/**
206
	 * Back-end widget form.
207
	 *
208
	 * @see WP_Widget::form()
209
	 *
210
	 * @param array $instance Previously saved values from database.
211
	 *
212
	 * @return string|void
213
	 */
214
	public function form( $instance ) {
215
		$instance = wp_parse_args( $instance, $this->defaults );
216
		$title    = sanitize_text_field( $instance['title'] );
217
		$sizes    = array(
218
			'small'  => __( 'Small', 'jetpack' ),
219
			'medium' => __( 'Medium', 'jetpack' ),
220
			'large'  => __( 'Large', 'jetpack' ),
221
		);
222
		$new_tab  = isset( $instance['new-tab'] ) ? (bool) $instance['new-tab'] : false;
223
		?>
224
225
		<p>
226
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
227
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
228
		</p>
229
230
		<p>
231
			<label for="<?php echo $this->get_field_id( 'icon-size' ); ?>"><?php esc_html_e( 'Size:', 'jetpack' ); ?></label>
232
			<select class="widefat" name="<?php echo $this->get_field_name( 'icon-size' ); ?>">
233
				<?php foreach ( $sizes as $value => $label ) : ?>
234
					<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['icon-size'] ); ?>><?php echo esc_attr( $label ); ?></option>
235
				<?php endforeach; ?>
236
			</select>
237
		</p>
238
239
		<div class="jetpack-social-icons-widget-list"
240
			data-url-icon-id="<?php echo $this->get_field_id( 'url-icons' ); ?>"
241
			data-url-icon-name="<?php echo $this->get_field_name( 'url-icons' ); ?>"
242
		>
243
244
			<?php
245
			foreach ( $instance['icons'] as $icon ) {
246
				self::render_icons_template(
247
					array(
248
						'url-icon-id'   => $this->get_field_id( 'url-icons' ),
249
						'url-icon-name' => $this->get_field_name( 'url-icons' ),
250
						'url-value'     => $icon['url'],
251
					)
252
				);
253
			}
254
			?>
255
256
		</div>
257
258
		<p class="jetpack-social-icons-widget add-button">
259
			<button type="button" class="button jetpack-social-icons-add-button">
260
				<?php esc_html_e( 'Add an icon', 'jetpack' ); ?>
261
			</button>
262
		</p>
263
264
		<?php
265
		switch ( get_locale() ) {
266
			case 'es':
267
				$support = 'https://es.support.wordpress.com/social-media-icons-widget/#iconos-disponibles';
268
				break;
269
270
			case 'pt-br':
271
				$support = 'https://br.support.wordpress.com/widgets/widget-de-icones-sociais/#ícones-disponíveis';
272
				break;
273
274
			default:
275
				$support = 'https://en.support.wordpress.com/widgets/social-media-icons-widget/#available-icons';
276
		}
277
		?>
278
279
		<p>
280
			<em><a href="<?php echo esc_url( $support ); ?>" target="_blank">
281
				<?php esc_html_e( 'View available icons', 'jetpack' ); ?>
282
			</a></em>
283
		</p>
284
285
		<p>
286
			<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'new-tab' ); ?>" name="<?php echo $this->get_field_name( 'new-tab' ); ?>" <?php checked( $new_tab ); ?> />
287
			<label for="<?php echo $this->get_field_id( 'new-tab' ); ?>"><?php esc_html_e( 'Open link in a new tab', 'jetpack' ); ?></label>
288
		</p>
289
290
	<?php
291
	}
292
293
	/**
294
	 * Generates template to add icons.
295
	 *
296
	 * @param array $args Template arguments
297
	 */
298
	static function render_icons_template( $args = array() ) {
299
		$defaults = array(
300
			'url-icon-id'   => '',
301
			'url-icon-name' => '',
302
			'url-value'     => '',
303
		);
304
305
		$args = wp_parse_args( $args, $defaults );
306
		?>
307
308
		<div class="jetpack-social-icons-widget-item">
309
			<div class="jetpack-social-icons-widget-item-wrapper">
310
				<div class="handle"></div>
311
312
				<p class="jetpack-widget-social-icons-url">
313
					<?php
314
						printf(
315
							'<input class="widefat id="%1$s" name="%2$s[]" type="text" placeholder="%3$s" value="%4$s"/>',
316
							esc_attr( $args['url-icon-id'] ),
317
							esc_attr( $args['url-icon-name'] ),
318
							esc_attr__( 'Account URL', 'jetpack' ),
319
							esc_url( $args['url-value'], array( 'http', 'https', 'mailto', 'skype' ) )
320
						);
321
					?>
322
				</p>
323
324
				<p class="jetpack-widget-social-icons-remove-item">
325
					<a class="jetpack-widget-social-icons-remove-item-button" href="javascript:;">
326
						<?php esc_html_e( 'Remove', 'jetpack' ); ?>
327
					</a>
328
				</p>
329
			</div>
330
		</div>
331
332
		<?php
333
	}
334
335
	/**
336
	 * Return SVG markup.
337
	 *
338
	 * @param array $args {
339
	 *     Parameters needed to display an SVG.
340
	 *
341
	 *     @type string $icon  Required SVG icon filename.
342
	 * }
343
	 * @return string SVG markup.
344
	 */
345
	public function get_svg_icon( $args = array() ) {
346
		// Make sure $args are an array.
347
		if ( empty( $args ) ) {
348
			return esc_html__( 'Please define default parameters in the form of an array.', 'jetpack' );
349
		}
350
351
		// Set defaults.
352
		$defaults = array(
353
			'icon' => '',
354
		);
355
356
		// Parse args.
357
		$args = wp_parse_args( $args, $defaults );
358
359
		// Define an icon.
360
		if ( false === array_key_exists( 'icon', $args ) ) {
361
			return esc_html__( 'Please define an SVG icon filename.', 'jetpack' );
362
		}
363
364
		// Set aria hidden.
365
		$aria_hidden = ' aria-hidden="true"';
366
367
		// Begin SVG markup.
368
		$svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . ' role="img">';
369
370
		/*
371
		 * Display the icon.
372
		 *
373
		 * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
374
		 *
375
		 * See https://core.trac.wordpress.org/ticket/38387.
376
		 */
377
		$svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
378
379
		$svg .= '</svg>';
380
381
		return $svg;
382
	}
383
384
	/**
385
	 * Returns an array of supported social links (URL, icon, and label).
386
	 *
387
	 * @return array $social_links_icons
388
	 */
389
	public function get_supported_icons() {
390
		$social_links_icons = array(
391
			array(
392
				'url'   => '500px.com',
393
				'icon'  => '500px',
394
				'label' => '500px',
395
			),
396
			array(
397
				'url'   => 'amazon.cn',
398
				'icon'  => 'amazon',
399
				'label' => 'Amazon',
400
			),
401
			array(
402
				'url'   => 'amazon.in',
403
				'icon'  => 'amazon',
404
				'label' => 'Amazon',
405
			),
406
			array(
407
				'url'   => 'amazon.fr',
408
				'icon'  => 'amazon',
409
				'label' => 'Amazon',
410
			),
411
			array(
412
				'url'   => 'amazon.de',
413
				'icon'  => 'amazon',
414
				'label' => 'Amazon',
415
			),
416
			array(
417
				'url'   => 'amazon.it',
418
				'icon'  => 'amazon',
419
				'label' => 'Amazon',
420
			),
421
			array(
422
				'url'   => 'amazon.nl',
423
				'icon'  => 'amazon',
424
				'label' => 'Amazon',
425
			),
426
			array(
427
				'url'   => 'amazon.es',
428
				'icon'  => 'amazon',
429
				'label' => 'Amazon',
430
			),
431
			array(
432
				'url'   => 'amazon.co',
433
				'icon'  => 'amazon',
434
				'label' => 'Amazon',
435
			),
436
			array(
437
				'url'   => 'amazon.ca',
438
				'icon'  => 'amazon',
439
				'label' => 'Amazon',
440
			),
441
			array(
442
				'url'   => 'amazon.com',
443
				'icon'  => 'amazon',
444
				'label' => 'Amazon',
445
			),
446
			array(
447
				'url'   => 'apple.com',
448
				'icon'  => 'apple',
449
				'label' => 'Apple',
450
			),
451
			array(
452
				'url'   => 'itunes.com',
453
				'icon'  => 'apple',
454
				'label' => 'iTunes',
455
			),
456
			array(
457
				'url'   => 'bandcamp.com',
458
				'icon'  => 'bandcamp',
459
				'label' => 'Bandcamp',
460
			),
461
			array(
462
				'url'   => 'behance.net',
463
				'icon'  => 'behance',
464
				'label' => 'Behance',
465
			),
466
			array(
467
				'url'   => 'codepen.io',
468
				'icon'  => 'codepen',
469
				'label' => 'CodePen',
470
			),
471
			array(
472
				'url'   => 'deviantart.com',
473
				'icon'  => 'deviantart',
474
				'label' => 'DeviantArt',
475
			),
476
			array(
477
				'url'   => 'digg.com',
478
				'icon'  => 'digg',
479
				'label' => 'Digg',
480
			),
481
			array(
482
				'url'   => 'dribbble.com',
483
				'icon'  => 'dribbble',
484
				'label' => 'Dribbble',
485
			),
486
			array(
487
				'url'   => 'dropbox.com',
488
				'icon'  => 'dropbox',
489
				'label' => 'Dropbox',
490
			),
491
			array(
492
				'url'   => 'etsy.com',
493
				'icon'  => 'etsy',
494
				'label' => 'Etsy',
495
			),
496
			array(
497
				'url'   => 'facebook.com',
498
				'icon'  => 'facebook',
499
				'label' => 'Facebook',
500
			),
501
			array(
502
				'url'   => '/feed/',
503
				'icon'  => 'feed',
504
				'label' => __( 'RSS Feed', 'jetpack' ),
505
			),
506
			array(
507
				'url'   => 'flickr.com',
508
				'icon'  => 'flickr',
509
				'label' => 'Flickr',
510
			),
511
			array(
512
				'url'   => 'foursquare.com',
513
				'icon'  => 'foursquare',
514
				'label' => 'Foursquare',
515
			),
516
			array(
517
				'url'   => 'goodreads.com',
518
				'icon'  => 'goodreads',
519
				'label' => 'Goodreads',
520
			),
521
			array(
522
				'url'   => 'google.com/+',
523
				'icon'  => 'google-plus',
524
				'label' => 'Google +',
525
			),
526
			array(
527
				'url'   => 'plus.google.com',
528
				'icon'  => 'google-plus',
529
				'label' => 'Google +',
530
			),
531
			array(
532
				'url'   => 'google.com',
533
				'icon'  => 'google',
534
				'label' => 'Google',
535
			),
536
			array(
537
				'url'   => 'github.com',
538
				'icon'  => 'github',
539
				'label' => 'GitHub',
540
			),
541
			array(
542
				'url'   => 'instagram.com',
543
				'icon'  => 'instagram',
544
				'label' => 'Instagram',
545
			),
546
			array(
547
				'url'   => 'linkedin.com',
548
				'icon'  => 'linkedin',
549
				'label' => 'LinkedIn',
550
			),
551
			array(
552
				'url'   => 'mailto:',
553
				'icon'  => 'mail',
554
				'label' => __( 'Email', 'jetpack' ),
555
			),
556
			array(
557
				'url'   => 'meetup.com',
558
				'icon'  => 'meetup',
559
				'label' => 'Meetup',
560
			),
561
			array(
562
				'url'   => 'medium.com',
563
				'icon'  => 'medium',
564
				'label' => 'Medium',
565
			),
566
			array(
567
				'url'   => 'pinterest.com',
568
				'icon'  => 'pinterest',
569
				'label' => 'Pinterest',
570
			),
571
			array(
572
				'url'   => 'getpocket.com',
573
				'icon'  => 'pocket',
574
				'label' => 'Pocket',
575
			),
576
			array(
577
				'url'   => 'reddit.com',
578
				'icon'  => 'reddit',
579
				'label' => 'Reddit',
580
			),
581
			array(
582
				'url'   => 'skype.com',
583
				'icon'  => 'skype',
584
				'label' => 'Skype',
585
			),
586
			array(
587
				'url'   => 'skype:',
588
				'icon'  => 'skype',
589
				'label' => 'Skype',
590
			),
591
			array(
592
				'url'   => 'slideshare.net',
593
				'icon'  => 'slideshare',
594
				'label' => 'SlideShare',
595
			),
596
			array(
597
				'url'   => 'snapchat.com',
598
				'icon'  => 'snapchat',
599
				'label' => 'Snapchat',
600
			),
601
			array(
602
				'url'   => 'soundcloud.com',
603
				'icon'  => 'soundcloud',
604
				'label' => 'SoundCloud',
605
			),
606
			array(
607
				'url'   => 'spotify.com',
608
				'icon'  => 'spotify',
609
				'label' => 'Spotify',
610
			),
611
			array(
612
				'url'   => 'stumbleupon.com',
613
				'icon'  => 'stumbleupon',
614
				'label' => 'StumbleUpon',
615
			),
616
			array(
617
				'url'   => 'tumblr.com',
618
				'icon'  => 'tumblr',
619
				'label' => 'Tumblr',
620
			),
621
			array(
622
				'url'   => 'twitch.tv',
623
				'icon'  => 'twitch',
624
				'label' => 'Twitch',
625
			),
626
			array(
627
				'url'   => 'twitter.com',
628
				'icon'  => 'twitter',
629
				'label' => 'Twitter',
630
			),
631
			array(
632
				'url'   => 'vimeo.com',
633
				'icon'  => 'vimeo',
634
				'label' => 'Vimeo',
635
			),
636
			array(
637
				'url'   => 'vk.com',
638
				'icon'  => 'vk',
639
				'label' => 'VK',
640
			),
641
			array(
642
				'url'   => 'wordpress.com',
643
				'icon'  => 'wordpress',
644
				'label' => 'WordPress.com',
645
			),
646
			array(
647
				'url'   => 'wordpress.org',
648
				'icon'  => 'wordpress',
649
				'label' => 'WordPress',
650
			),
651
			array(
652
				'url'   => 'yelp.com',
653
				'icon'  => 'yelp',
654
				'label' => 'Yelp',
655
			),
656
			array(
657
				'url'   => 'youtube.com',
658
				'icon'  => 'youtube',
659
				'label' => 'YouTube',
660
			),
661
		);
662
663
		return $social_links_icons;
664
	}
665
} // Jetpack_Widget_Social_Icons
666
667
/**
668
 * Register and load the widget.
669
 *
670
 * @access public
671
 * @return void
672
 */
673
function jetpack_widget_social_icons_load() {
674
	register_widget( 'Jetpack_Widget_Social_Icons' );
675
}
676
add_action( 'widgets_init', 'jetpack_widget_social_icons_load' );
677