PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.6.2
Jetpack – WP Security, Backup, Speed, & Growth v7.6.2
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
jetpack / modules / widgets / social-icons.php

social-icons.php in Jetpack – WP Security, Backup, Speed, & Growth 7.6.2, at modules/widgets/social-icons.php

681 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 global $pagenow;
13
14 $widget_ops = array(
15 'classname' => 'jetpack_widget_social_icons',
16 'description' => __( 'Add social-media icons to your site.', 'jetpack' ),
17 'customize_selective_refresh' => true,
18 );
19
20 parent::__construct(
21 'jetpack_widget_social_icons',
22 /** This filter is documented in modules/widgets/facebook-likebox.php */
23 apply_filters( 'jetpack_widget_name', __( 'Social Icons', 'jetpack' ) ),
24 $widget_ops
25 );
26
27 $this->defaults = array(
28 'title' => __( 'Follow Us', 'jetpack' ),
29 'icon-size' => 'medium',
30 'new-tab' => false,
31 'icons' => array(
32 array(
33 'url' => '',
34 ),
35 ),
36 );
37
38 // Enqueue admin scrips and styles, only in the customizer or the old widgets page.
39 if ( is_customize_preview() || 'widgets.php' === $pagenow ) {
40 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
41 add_action( 'admin_print_footer_scripts', array( $this, 'render_admin_js' ) );
42 }
43
44 // Enqueue scripts and styles for the display of the widget, on the frontend or in the customizer.
45 if ( is_active_widget( false, $this->id, $this->id_base, true ) || is_customize_preview() ) {
46 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_icon_scripts' ) );
47 add_action( 'wp_footer', array( $this, 'include_svg_icons' ), 9999 );
48 }
49 }
50
51 /**
52 * Script & styles for admin widget form.
53 */
54 public function enqueue_admin_scripts() {
55 wp_enqueue_script( 'jetpack-widget-social-icons-script', plugins_url( 'social-icons/social-icons-admin.js', __FILE__ ), array( 'jquery-ui-sortable' ), '20170506' );
56 wp_enqueue_style( 'jetpack-widget-social-icons-admin', plugins_url( 'social-icons/social-icons-admin.css', __FILE__ ), array(), '20170506' );
57 }
58
59 /**
60 * Styles for front-end widget.
61 */
62 public function enqueue_icon_scripts() {
63 wp_enqueue_style( 'jetpack-widget-social-icons-styles', plugins_url( 'social-icons/social-icons.css', __FILE__ ), array(), '20170506' );
64 }
65
66 /**
67 * JavaScript for admin widget form.
68 */
69 public function render_admin_js() {
70 ?>
71 <script type="text/html" id="tmpl-jetpack-widget-social-icons-template">
72 <?php self::render_icons_template(); ?>
73 </script>
74 <?php
75 }
76
77 /**
78 * Add SVG definitions to the footer.
79 */
80 public function include_svg_icons() {
81 // Define SVG sprite file in Jetpack
82 $svg_icons = dirname( dirname( __FILE__ ) ) . '/theme-tools/social-menu/social-menu.svg';
83
84 // Define SVG sprite file in WPCOM
85 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
86 $svg_icons = dirname( dirname( __FILE__ ) ) . '/social-menu/social-menu.svg';
87 }
88
89 // If it exists, include it.
90 if ( is_file( $svg_icons ) ) {
91 require_once( $svg_icons );
92 }
93 }
94
95 /**
96 * Front-end display of widget.
97 *
98 * @see WP_Widget::widget()
99 *
100 * @param array $args Widget arguments.
101 * @param array $instance Saved values from database.
102 */
103 public function widget( $args, $instance ) {
104 $instance = wp_parse_args( $instance, $this->defaults );
105
106 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
107 $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
108
109 echo $args['before_widget'];
110
111 if ( ! empty( $title ) ) {
112 echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
113 }
114
115 if ( ! empty( $instance['icons'] ) ) :
116
117 // Get supported social icons.
118 $social_icons = $this->get_supported_icons();
119 $default_icon = $this->get_svg_icon( array( 'icon' => 'chain' ) );
120
121 // Set target attribute for the link
122 if ( true === $instance['new-tab'] ) {
123 $target = '_blank';
124 } else {
125 $target = '_self';
126 }
127 ?>
128
129 <ul class="jetpack-social-widget-list size-<?php echo esc_attr( $instance['icon-size'] ); ?>">
130
131 <?php foreach ( $instance['icons'] as $icon ) : ?>
132
133 <?php if ( ! empty( $icon['url'] ) ) : ?>
134 <li class="jetpack-social-widget-item">
135 <a href="<?php echo esc_url( $icon['url'], array( 'http', 'https', 'mailto', 'skype' ) ); ?>" target="<?php echo $target; ?>">
136 <?php
137 $found_icon = false;
138
139 foreach ( $social_icons as $social_icon ) {
140 if ( false !== stripos( $icon['url'], $social_icon['url'] ) ) {
141 echo '<span class="screen-reader-text">' . esc_attr( $social_icon['label'] ) . '</span>';
142 echo $this->get_svg_icon( array( 'icon' => esc_attr( $social_icon['icon'] ) ) );
143 $found_icon = true;
144 break;
145 }
146 }
147
148 if ( ! $found_icon ) {
149 echo $default_icon;
150 }
151 ?>
152 </a>
153 </li>
154 <?php endif; ?>
155
156 <?php endforeach; ?>
157
158 </ul>
159
160 <?php
161 endif;
162
163 echo $args['after_widget'];
164
165 /** This action is documented in modules/widgets/gravatar-profile.php */
166 do_action( 'jetpack_stats_extra', 'widget_view', 'social_icons' );
167 }
168
169 /**
170 * Sanitize widget form values as they are saved.
171 *
172 * @see WP_Widget::update()
173 *
174 * @param array $new_instance Values just sent to be saved.
175 * @param array $old_instance Previously saved values from database.
176 *
177 * @return array Updated safe values to be saved.
178 */
179 public function update( $new_instance, $old_instance ) {
180 $instance['title'] = sanitize_text_field( $new_instance['title'] );
181 $instance['icon-size'] = $this->defaults['icon-size'];
182
183 if ( in_array( $new_instance['icon-size'], array( 'small', 'medium', 'large' ) ) ) {
184 $instance['icon-size'] = $new_instance['icon-size'];
185 }
186
187 $instance['new-tab'] = isset( $new_instance['new-tab'] ) ? (bool) $new_instance['new-tab'] : false;
188 $icon_count = count( $new_instance['url-icons'] );
189 $instance['icons'] = array();
190
191 foreach ( $new_instance['url-icons'] as $url ) {
192 $url = filter_var( $url, FILTER_SANITIZE_URL );
193
194 if ( ! empty( $url ) ) {
195 $instance['icons'][] = array(
196 'url' => $url,
197 );
198 }
199 }
200
201 return $instance;
202 }
203
204 /**
205 * Back-end widget form.
206 *
207 * @see WP_Widget::form()
208 *
209 * @param array $instance Previously saved values from database.
210 *
211 * @return string|void
212 */
213 public function form( $instance ) {
214 $instance = wp_parse_args( $instance, $this->defaults );
215 $title = sanitize_text_field( $instance['title'] );
216 $sizes = array(
217 'small' => __( 'Small', 'jetpack' ),
218 'medium' => __( 'Medium', 'jetpack' ),
219 'large' => __( 'Large', 'jetpack' ),
220 );
221 $new_tab = isset( $instance['new-tab'] ) ? (bool) $instance['new-tab'] : false;
222 ?>
223
224 <p>
225 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
226 <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 ); ?>" />
227 </p>
228
229 <p>
230 <label for="<?php echo $this->get_field_id( 'icon-size' ); ?>"><?php esc_html_e( 'Size:', 'jetpack' ); ?></label>
231 <select class="widefat" name="<?php echo $this->get_field_name( 'icon-size' ); ?>">
232 <?php foreach ( $sizes as $value => $label ) : ?>
233 <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['icon-size'] ); ?>><?php echo esc_attr( $label ); ?></option>
234 <?php endforeach; ?>
235 </select>
236 </p>
237
238 <div class="jetpack-social-icons-widget-list"
239 data-url-icon-id="<?php echo $this->get_field_id( 'url-icons' ); ?>"
240 data-url-icon-name="<?php echo $this->get_field_name( 'url-icons' ); ?>"
241 >
242
243 <?php
244 foreach ( $instance['icons'] as $icon ) {
245 self::render_icons_template(
246 array(
247 'url-icon-id' => $this->get_field_id( 'url-icons' ),
248 'url-icon-name' => $this->get_field_name( 'url-icons' ),
249 'url-value' => $icon['url'],
250 )
251 );
252 }
253 ?>
254
255 </div>
256
257 <p class="jetpack-social-icons-widget add-button">
258 <button type="button" class="button jetpack-social-icons-add-button">
259 <?php esc_html_e( 'Add an icon', 'jetpack' ); ?>
260 </button>
261 </p>
262
263 <?php
264 switch ( get_locale() ) {
265 case 'es':
266 $support = 'https://es.support.wordpress.com/social-media-icons-widget/#iconos-disponibles';
267 break;
268
269 case 'pt-br':
270 $support = 'https://br.support.wordpress.com/widgets/widget-de-icones-sociais/#ícones-disponíveis';
271 break;
272
273 default:
274 $support = 'https://en.support.wordpress.com/widgets/social-media-icons-widget/#available-icons';
275 }
276 ?>
277
278 <p>
279 <em><a href="<?php echo esc_url( $support ); ?>" target="_blank">
280 <?php esc_html_e( 'View available icons', 'jetpack' ); ?>
281 </a></em>
282 </p>
283
284 <p>
285 <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 ); ?> />
286 <label for="<?php echo $this->get_field_id( 'new-tab' ); ?>"><?php esc_html_e( 'Open link in a new tab', 'jetpack' ); ?></label>
287 </p>
288
289 <?php
290 }
291
292 /**
293 * Generates template to add icons.
294 *
295 * @param array $args Template arguments
296 */
297 static function render_icons_template( $args = array() ) {
298 $defaults = array(
299 'url-icon-id' => '',
300 'url-icon-name' => '',
301 'url-value' => '',
302 );
303
304 $args = wp_parse_args( $args, $defaults );
305 ?>
306
307 <div class="jetpack-social-icons-widget-item">
308 <div class="jetpack-social-icons-widget-item-wrapper">
309 <div class="handle"></div>
310
311 <p class="jetpack-widget-social-icons-url">
312 <?php
313 printf(
314 '<input class="widefat" id="%1$s" name="%2$s[]" type="text" placeholder="%3$s" value="%4$s"/>',
315 esc_attr( $args['url-icon-id'] ),
316 esc_attr( $args['url-icon-name'] ),
317 esc_attr__( 'Account URL', 'jetpack' ),
318 esc_url( $args['url-value'], array( 'http', 'https', 'mailto', 'skype' ) )
319 );
320 ?>
321 </p>
322
323 <p class="jetpack-widget-social-icons-remove-item">
324 <a class="jetpack-widget-social-icons-remove-item-button" href="javascript:;">
325 <?php esc_html_e( 'Remove', 'jetpack' ); ?>
326 </a>
327 </p>
328 </div>
329 </div>
330
331 <?php
332 }
333
334 /**
335 * Return SVG markup.
336 *
337 * @param array $args {
338 * Parameters needed to display an SVG.
339 *
340 * @type string $icon Required SVG icon filename.
341 * }
342 * @return string SVG markup.
343 */
344 public function get_svg_icon( $args = array() ) {
345 // Make sure $args are an array.
346 if ( empty( $args ) ) {
347 return esc_html__( 'Please define default parameters in the form of an array.', 'jetpack' );
348 }
349
350 // Set defaults.
351 $defaults = array(
352 'icon' => '',
353 );
354
355 // Parse args.
356 $args = wp_parse_args( $args, $defaults );
357
358 // Define an icon.
359 if ( false === array_key_exists( 'icon', $args ) ) {
360 return esc_html__( 'Please define an SVG icon filename.', 'jetpack' );
361 }
362
363 // Set aria hidden.
364 $aria_hidden = ' aria-hidden="true"';
365
366 // Begin SVG markup.
367 $svg = '<svg class="icon icon-' . esc_attr( $args['icon'] ) . '"' . $aria_hidden . ' role="presentation">';
368
369 /*
370 * Display the icon.
371 *
372 * The whitespace around `<use>` is intentional - it is a work around to a keyboard navigation bug in Safari 10.
373 *
374 * See https://core.trac.wordpress.org/ticket/38387.
375 */
376 $svg .= ' <use href="#icon-' . esc_html( $args['icon'] ) . '" xlink:href="#icon-' . esc_html( $args['icon'] ) . '"></use> ';
377
378 $svg .= '</svg>';
379
380 return $svg;
381 }
382
383 /**
384 * Returns an array of supported social links (URL, icon, and label).
385 *
386 * @return array $social_links_icons
387 */
388 public function get_supported_icons() {
389 $social_links_icons = array(
390 array(
391 'url' => '500px.com',
392 'icon' => '500px',
393 'label' => '500px',
394 ),
395 array(
396 'url' => 'amazon.cn',
397 'icon' => 'amazon',
398 'label' => 'Amazon',
399 ),
400 array(
401 'url' => 'amazon.in',
402 'icon' => 'amazon',
403 'label' => 'Amazon',
404 ),
405 array(
406 'url' => 'amazon.fr',
407 'icon' => 'amazon',
408 'label' => 'Amazon',
409 ),
410 array(
411 'url' => 'amazon.de',
412 'icon' => 'amazon',
413 'label' => 'Amazon',
414 ),
415 array(
416 'url' => 'amazon.it',
417 'icon' => 'amazon',
418 'label' => 'Amazon',
419 ),
420 array(
421 'url' => 'amazon.nl',
422 'icon' => 'amazon',
423 'label' => 'Amazon',
424 ),
425 array(
426 'url' => 'amazon.es',
427 'icon' => 'amazon',
428 'label' => 'Amazon',
429 ),
430 array(
431 'url' => 'amazon.co',
432 'icon' => 'amazon',
433 'label' => 'Amazon',
434 ),
435 array(
436 'url' => 'amazon.ca',
437 'icon' => 'amazon',
438 'label' => 'Amazon',
439 ),
440 array(
441 'url' => 'amazon.com',
442 'icon' => 'amazon',
443 'label' => 'Amazon',
444 ),
445 array(
446 'url' => 'apple.com',
447 'icon' => 'apple',
448 'label' => 'Apple',
449 ),
450 array(
451 'url' => 'itunes.com',
452 'icon' => 'apple',
453 'label' => 'iTunes',
454 ),
455 array(
456 'url' => 'bandcamp.com',
457 'icon' => 'bandcamp',
458 'label' => 'Bandcamp',
459 ),
460 array(
461 'url' => 'behance.net',
462 'icon' => 'behance',
463 'label' => 'Behance',
464 ),
465 array(
466 'url' => 'codepen.io',
467 'icon' => 'codepen',
468 'label' => 'CodePen',
469 ),
470 array(
471 'url' => 'deviantart.com',
472 'icon' => 'deviantart',
473 'label' => 'DeviantArt',
474 ),
475 array(
476 'url' => 'digg.com',
477 'icon' => 'digg',
478 'label' => 'Digg',
479 ),
480 array(
481 'url' => 'discord.gg',
482 'icon' => 'discord',
483 'label' => 'Discord',
484 ),
485 array(
486 'url' => 'discordapp.com',
487 'icon' => 'discord',
488 'label' => 'Discord',
489 ),
490 array(
491 'url' => 'dribbble.com',
492 'icon' => 'dribbble',
493 'label' => 'Dribbble',
494 ),
495 array(
496 'url' => 'dropbox.com',
497 'icon' => 'dropbox',
498 'label' => 'Dropbox',
499 ),
500 array(
501 'url' => 'etsy.com',
502 'icon' => 'etsy',
503 'label' => 'Etsy',
504 ),
505 array(
506 'url' => 'facebook.com',
507 'icon' => 'facebook',
508 'label' => 'Facebook',
509 ),
510 array(
511 'url' => '/feed/',
512 'icon' => 'feed',
513 'label' => __( 'RSS Feed', 'jetpack' ),
514 ),
515 array(
516 'url' => 'flickr.com',
517 'icon' => 'flickr',
518 'label' => 'Flickr',
519 ),
520 array(
521 'url' => 'foursquare.com',
522 'icon' => 'foursquare',
523 'label' => 'Foursquare',
524 ),
525 array(
526 'url' => 'goodreads.com',
527 'icon' => 'goodreads',
528 'label' => 'Goodreads',
529 ),
530 array(
531 'url' => 'google.com',
532 'icon' => 'google',
533 'label' => 'Google',
534 ),
535 array(
536 'url' => 'github.com',
537 'icon' => 'github',
538 'label' => 'GitHub',
539 ),
540 array(
541 'url' => 'instagram.com',
542 'icon' => 'instagram',
543 'label' => 'Instagram',
544 ),
545 array(
546 'url' => 'linkedin.com',
547 'icon' => 'linkedin',
548 'label' => 'LinkedIn',
549 ),
550 array(
551 'url' => 'mailto:',
552 'icon' => 'mail',
553 'label' => __( 'Email', 'jetpack' ),
554 ),
555 array(
556 'url' => 'meetup.com',
557 'icon' => 'meetup',
558 'label' => 'Meetup',
559 ),
560 array(
561 'url' => 'medium.com',
562 'icon' => 'medium',
563 'label' => 'Medium',
564 ),
565 array(
566 'url' => 'pinterest.',
567 'icon' => 'pinterest',
568 'label' => 'Pinterest',
569 ),
570 array(
571 'url' => 'getpocket.com',
572 'icon' => 'pocket',
573 'label' => 'Pocket',
574 ),
575 array(
576 'url' => 'reddit.com',
577 'icon' => 'reddit',
578 'label' => 'Reddit',
579 ),
580 array(
581 'url' => 'skype.com',
582 'icon' => 'skype',
583 'label' => 'Skype',
584 ),
585 array(
586 'url' => 'skype:',
587 'icon' => 'skype',
588 'label' => 'Skype',
589 ),
590 array(
591 'url' => 'slideshare.net',
592 'icon' => 'slideshare',
593 'label' => 'SlideShare',
594 ),
595 array(
596 'url' => 'snapchat.com',
597 'icon' => 'snapchat',
598 'label' => 'Snapchat',
599 ),
600 array(
601 'url' => 'soundcloud.com',
602 'icon' => 'soundcloud',
603 'label' => 'SoundCloud',
604 ),
605 array(
606 'url' => 'spotify.com',
607 'icon' => 'spotify',
608 'label' => 'Spotify',
609 ),
610 array(
611 'url' => 'stackoverflow.com',
612 'icon' => 'stackoverflow',
613 'label' => 'Stack Overflow',
614 ),
615 array(
616 'url' => 'stumbleupon.com',
617 'icon' => 'stumbleupon',
618 'label' => 'StumbleUpon',
619 ),
620 array(
621 'url' => 'tumblr.com',
622 'icon' => 'tumblr',
623 'label' => 'Tumblr',
624 ),
625 array(
626 'url' => 'twitch.tv',
627 'icon' => 'twitch',
628 'label' => 'Twitch',
629 ),
630 array(
631 'url' => 'twitter.com',
632 'icon' => 'twitter',
633 'label' => 'Twitter',
634 ),
635 array(
636 'url' => 'vimeo.com',
637 'icon' => 'vimeo',
638 'label' => 'Vimeo',
639 ),
640 array(
641 'url' => 'vk.com',
642 'icon' => 'vk',
643 'label' => 'VK',
644 ),
645 array(
646 'url' => 'wordpress.com',
647 'icon' => 'wordpress',
648 'label' => 'WordPress.com',
649 ),
650 array(
651 'url' => 'wordpress.org',
652 'icon' => 'wordpress',
653 'label' => 'WordPress',
654 ),
655 array(
656 'url' => 'yelp.com',
657 'icon' => 'yelp',
658 'label' => 'Yelp',
659 ),
660 array(
661 'url' => 'youtube.com',
662 'icon' => 'youtube',
663 'label' => 'YouTube',
664 ),
665 );
666
667 return $social_links_icons;
668 }
669 } // Jetpack_Widget_Social_Icons
670
671 /**
672 * Register and load the widget.
673 *
674 * @access public
675 * @return void
676 */
677 function jetpack_widget_social_icons_load() {
678 register_widget( 'Jetpack_Widget_Social_Icons' );
679 }
680 add_action( 'widgets_init', 'jetpack_widget_social_icons_load' );
681