/modules/widgets/social-media-icons.php

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