WPCOM_social_media_icons_widget

WPCOM_social_media_icons_widget class.

Defined (1)

The class is defined in the following location(s).

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