social-media-icons.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. This widget is now deprecated.
  7. Any new features should go into modules/widgets/social-icons.php instead.
  8. @see https://github.com/Automattic/jetpack/pull/8498
  9. */
  10. /**
  11. * WPCOM_social_media_icons_widget class.
  12. *
  13. * @extends WP_Widget
  14. */
  15. class WPCOM_social_media_icons_widget extends WP_Widget {
  16. /**
  17. * Defaults
  18. *
  19. * @var mixed
  20. * @access private
  21. */
  22. private $defaults;
  23. /**
  24. * Services
  25. *
  26. * @var mixed
  27. * @access private
  28. */
  29. private $services;
  30. /**
  31. * __construct function.
  32. *
  33. * @access public
  34. * @return void
  35. */
  36. public function __construct() {
  37. parent::__construct(
  38. 'wpcom_social_media_icons_widget',
  39. /** This filter is documented in modules/widgets/facebook-likebox.php */
  40. apply_filters( 'jetpack_widget_name', esc_html__( 'Social Media Icons (Deprecated)', 'jetpack' ) ),
  41. array(
  42. 'description' => __( 'A simple widget that displays social media icons.', 'jetpack' ),
  43. 'customize_selective_refresh' => true,
  44. )
  45. );
  46. $this->defaults = array(
  47. 'title' => __( 'Social', 'jetpack' ),
  48. 'facebook_username' => '',
  49. 'twitter_username' => '',
  50. 'instagram_username' => '',
  51. 'pinterest_username' => '',
  52. 'linkedin_username' => '',
  53. 'github_username' => '',
  54. 'youtube_username' => '',
  55. 'vimeo_username' => '',
  56. 'googleplus_username' => '',
  57. 'flickr_username' => '',
  58. 'wordpress_username' => '',
  59. 'twitch_username' => '',
  60. 'tumblr_username' => '',
  61. );
  62. $this->services = array(
  63. 'facebook' => array( 'Facebook', 'https://www.facebook.com/%s/' ),
  64. 'twitter' => array( 'Twitter', 'https://twitter.com/%s/' ),
  65. 'instagram' => array( 'Instagram', 'https://www.instagram.com/%s/' ),
  66. 'pinterest' => array( 'Pinterest', 'https://www.pinterest.com/%s/' ),
  67. 'linkedin' => array( 'LinkedIn', 'https://www.linkedin.com/in/%s/' ),
  68. 'github' => array( 'GitHub', 'https://github.com/%s/' ),
  69. 'youtube' => array( 'YouTube', 'https://www.youtube.com/%s/' ),
  70. 'vimeo' => array( 'Vimeo', 'https://vimeo.com/%s/' ),
  71. 'googleplus' => array( 'Google+', 'https://plus.google.com/u/0/%s/' ),
  72. 'flickr' => array( 'Flickr', 'https://www.flickr.com/photos/%s/' ),
  73. 'wordpress' => array( 'WordPress.org', 'https://profiles.wordpress.org/%s/' ),
  74. 'twitch' => array( 'Twitch', 'https://www.twitch.tv/%s/' ),
  75. 'tumblr' => array( 'Tumblr', 'https://%s.tumblr.com' ),
  76. );
  77. if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
  78. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
  79. }
  80. }
  81. /**
  82. * Enqueue Style.
  83. *
  84. * @access public
  85. * @return void
  86. */
  87. public function enqueue_style() {
  88. wp_register_style( 'jetpack_social_media_icons_widget', plugins_url( 'social-media-icons/style.css', __FILE__ ), array(), '20150602' );
  89. wp_enqueue_style( 'jetpack_social_media_icons_widget' );
  90. }
  91. /**
  92. * Check Genericons.
  93. *
  94. * @access private
  95. * @return Bool.
  96. */
  97. private function check_genericons() {
  98. global $wp_styles;
  99. foreach ( $wp_styles->queue as $handle ) {
  100. if ( false !== stristr( $handle, 'genericons' ) ) {
  101. return $handle;
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * Widget Front End.
  108. *
  109. * @access public
  110. * @param mixed $args Arguments.
  111. * @param mixed $instance Instance.
  112. * @return void
  113. */
  114. public function widget( $args, $instance ) {
  115. $instance = wp_parse_args( (array) $instance, $this->defaults );
  116. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  117. $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
  118. if ( ! $this->check_genericons() ) {
  119. wp_enqueue_style( 'genericons' );
  120. }
  121. $index = 10;
  122. $html = array();
  123. $alt_text = esc_attr__( 'View %1$s&#8217;s profile on %2$s', 'jetpack' );
  124. foreach ( $this->services as $service => $data ) {
  125. list( $service_name, $url ) = $data;
  126. if ( ! isset( $instance[ $service . '_username' ] ) ) {
  127. continue;
  128. }
  129. $username = $link_username = $instance[ $service . '_username' ];
  130. if ( empty( $username ) ) {
  131. continue;
  132. }
  133. $index += 10;
  134. $predefined_url = false;
  135. /** Check if full URL entered in configuration, use it instead of tinkering **/
  136. if (
  137. in_array(
  138. parse_url( $username, PHP_URL_SCHEME ),
  139. array( 'http', 'https' )
  140. )
  141. ) {
  142. $predefined_url = $username;
  143. // In case of a predefined link we only display the service name
  144. // for screen readers
  145. $alt_text = '%2$s';
  146. }
  147. if ( 'googleplus' === $service
  148. && ! is_numeric( $username )
  149. && substr( $username, 0, 1 ) !== '+'
  150. ) {
  151. $link_username = '+' . $username;
  152. }
  153. if ( 'youtube' === $service && 'UC' === substr( $username, 0, 2 ) ) {
  154. $link_username = 'channel/' . $username;
  155. } else if ( 'youtube' === $service ) {
  156. $link_username = 'user/' . $username;
  157. }
  158. if ( ! $predefined_url ) {
  159. $predefined_url = sprintf( $url, $link_username );
  160. }
  161. /**
  162. * Fires for each profile link in the social icons widget. Can be used
  163. * to change the links for certain social networks if needed. All URLs
  164. * will be passed through `esc_attr` on output.
  165. *
  166. * @module widgets
  167. *
  168. * @since 3.8.0
  169. *
  170. * @param string $url the currently processed URL
  171. * @param string $service the lowercase service slug, e.g. 'facebook', 'youtube', etc.
  172. */
  173. $link = apply_filters(
  174. 'jetpack_social_media_icons_widget_profile_link',
  175. $predefined_url,
  176. $service
  177. );
  178. $html[ $index ] = sprintf(
  179. '<a href="%1$s" class="genericon genericon-%2$s" target="_blank"><span class="screen-reader-text">%3$s</span></a>',
  180. esc_attr( $link ),
  181. esc_attr( $service ),
  182. sprintf( $alt_text, esc_html( $username ), $service_name )
  183. );
  184. }
  185. /**
  186. * Fires at the end of the list of Social Media accounts.
  187. * Can be used to add a new Social Media Site to the Social Media Icons Widget.
  188. * The filter function passed the array of HTML entries that will be sorted
  189. * by key, each wrapped in a list item element and output as an unsorted list.
  190. *
  191. * @module widgets
  192. *
  193. * @since 3.8.0
  194. *
  195. * @param array $html Associative array of HTML snippets per each icon.
  196. */
  197. $html = apply_filters( 'jetpack_social_media_icons_widget_array', $html );
  198. ksort( $html );
  199. $html = '<ul><li>' . join( '</li><li>', $html ) . '</li></ul>';
  200. if ( ! empty( $instance['title'] ) ) {
  201. $html = $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'] . $html;
  202. }
  203. $html = $args['before_widget'] . $html . $args['after_widget'];
  204. /** This action is documented in modules/widgets/gravatar-profile.php */
  205. do_action( 'jetpack_stats_extra', 'widget_view', 'social_media_icons' );
  206. /**
  207. * Filters the Social Media Icons widget output.
  208. *
  209. * @module widgets
  210. *
  211. * @since 3.6.0
  212. *
  213. * @param string $html Social Media Icons widget html output.
  214. */
  215. echo apply_filters( 'jetpack_social_media_icons_widget_output', $html );
  216. }
  217. /**
  218. * Widget Settings.
  219. *
  220. * @access public
  221. * @param mixed $instance Instance.
  222. * @return void
  223. */
  224. public function form( $instance ) {
  225. $instance = wp_parse_args( (array) $instance, $this->defaults );
  226. ?>
  227. <p>
  228. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'jetpack' ); ?></label>
  229. <input
  230. class="widefat"
  231. id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
  232. name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
  233. type="text"
  234. value="<?php echo esc_attr( $instance['title'] ); ?>"
  235. />
  236. </p>
  237. <?php
  238. foreach ( $this->services as $service => $data ) {
  239. list( $service_name, $url ) = $data;
  240. ?>
  241. <p>
  242. <label for="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>">
  243. <?php
  244. /* translators: %s is a social network name, e.g. Facebook. */
  245. printf( __( '%s username:', 'jetpack' ), $service_name );
  246. ?>
  247. </label>
  248. <input
  249. class="widefat"
  250. id="<?php echo esc_attr( $this->get_field_id( $service . '_username' ) ); ?>"
  251. name="<?php echo esc_attr( $this->get_field_name( $service . '_username' ) ); ?>"
  252. type="text"
  253. value="<?php echo esc_attr( $instance[ $service . '_username' ] ); ?>"
  254. />
  255. </p>
  256. <?php
  257. }
  258. }
  259. /**
  260. * Update Widget Settings.
  261. *
  262. * @access public
  263. * @param mixed $new_instance New Instance.
  264. * @param mixed $old_instance Old Instance.
  265. * @return Instance.
  266. */
  267. public function update( $new_instance, $old_instance ) {
  268. $instance = (array) $old_instance;
  269. foreach ( $new_instance as $field => $value ) {
  270. $instance[ $field ] = sanitize_text_field( $new_instance[ $field ] );
  271. }
  272. // Stats.
  273. $stats = $instance;
  274. unset( $stats['title'] );
  275. $stats = array_filter( $stats );
  276. $stats = array_keys( $stats );
  277. $stats = array_map( array( $this, 'remove_username' ), $stats );
  278. foreach ( $stats as $val ) {
  279. /**
  280. * Fires for each Social Media account being saved in the Social Media Widget settings.
  281. *
  282. * @module widgets
  283. *
  284. * @since 3.6.0
  285. *
  286. * @param string social-media-links-widget-svcs Type of action to track.
  287. * @param string $val Name of the Social Media account being saved.
  288. */
  289. do_action( 'jetpack_bump_stats_extras', 'social-media-links-widget-svcs', $val );
  290. }
  291. return $instance;
  292. }
  293. /**
  294. * Remove username from value before to save stats.
  295. *
  296. * @access public
  297. * @param mixed $val Value.
  298. * @return Value.
  299. */
  300. public function remove_username( $val ) {
  301. return str_replace( '_username', '', $val );
  302. }
  303. } // End Class.
  304. /**
  305. * Register and load the widget.
  306. *
  307. * @access public
  308. * @return void
  309. */
  310. function wpcom_social_media_icons_widget_load_widget() {
  311. $transient = 'wpcom_social_media_icons_widget::is_active';
  312. $has_widget = get_transient( $transient );
  313. if ( false === $has_widget ) {
  314. $is_active_widget = is_active_widget( false, false, 'wpcom_social_media_icons_widget', false );
  315. $has_widget = (int) ! empty( $is_active_widget );
  316. set_transient( $transient, $has_widget, 1 * HOUR_IN_SECONDS );
  317. }
  318. // [DEPRECATION]: Only register widget if active widget exists already
  319. if ( $has_widget ) {
  320. register_widget( 'wpcom_social_media_icons_widget' );
  321. }
  322. }
  323. add_action( 'widgets_init', 'wpcom_social_media_icons_widget_load_widget' );