PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/widgets/social-media-icons.php

https://gitlab.com/Gashler/sg
PHP | 207 lines | 141 code | 35 blank | 31 comment | 12 complexity | 2e9004abc4b1b5f083a1952810b40563 MD5 | raw file
  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. // Creating the widget
  8. class WPCOM_social_media_icons_widget extends WP_Widget {
  9. private $defaults;
  10. public function __construct() {
  11. parent::__construct(
  12. 'wpcom_social_media_icons_widget',
  13. apply_filters( 'jetpack_widget_name', esc_html__( 'Social Media Icons', 'jetpack' ) ),
  14. array( 'description' => __( 'A simple widget that displays social media icons.', 'jetpack' ), )
  15. );
  16. $this->defaults = array(
  17. 'title' => __( 'Social', 'jetpack' ),
  18. 'facebook_username' => '',
  19. 'twitter_username' => '',
  20. 'instagram_username' => '',
  21. 'pinterest_username' => '',
  22. 'linkedin_username' => '',
  23. 'github_username' => '',
  24. 'youtube_username' => '',
  25. 'vimeo_username' => '',
  26. );
  27. if ( is_active_widget( false, false, $this->id_base ) ) {
  28. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
  29. }
  30. }
  31. public function enqueue_style() {
  32. wp_register_style( 'jetpack_social_media_icons_widget', plugins_url( 'social-media-icons/style.css', __FILE__ ), array(), '20150602' );
  33. wp_enqueue_style( 'jetpack_social_media_icons_widget' );
  34. }
  35. private function check_genericons() {
  36. global $wp_styles;
  37. foreach ( $wp_styles->queue as $handle ) {
  38. if ( false !== stristr( $handle, 'genericons' ) ) {
  39. return $handle;
  40. }
  41. }
  42. return false;
  43. }
  44. // front end
  45. public function widget( $args, $instance ) {
  46. $instance = wp_parse_args( (array) $instance, $this->defaults );
  47. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  48. $instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
  49. if ( ! $this->check_genericons() ) {
  50. wp_enqueue_style( 'genericons' );
  51. }
  52. // before widget arguments
  53. $html = $args['before_widget'];
  54. if ( ! empty( $instance['title'] ) ) {
  55. $html .= $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'];
  56. }
  57. // display output
  58. $html .= '<ul>';
  59. $alt_text = esc_attr__( 'View %1$s&#8217;s profile on %2$s', 'jetpack' );
  60. if ( ! empty( $instance['facebook_username'] ) ) {
  61. $html .= '<li><a title="' . sprintf( $alt_text, esc_attr( $instance['facebook_username'] ), 'Facebook' ) . '" href="' . esc_url( 'https://www.facebook.com/' . $instance['facebook_username'] . '/' ) . '" class="genericon genericon-facebook" target="_blank"><span class="screen-reader-text">' . sprintf( $alt_text, esc_html( $instance['facebook_username'] ), 'Facebook' ) . '</span></a></li>';
  62. }
  63. if ( ! empty( $instance['twitter_username'] ) ) {
  64. $html .= '<li><a title="' . sprintf( $alt_text, esc_attr( $instance['twitter_username'] ), 'Twitter' ) . '" href="' . esc_url( 'https://twitter.com/' . $instance['twitter_username'] . '/' ) . '" class="genericon genericon-twitter" target="_blank"><span class="screen-reader-text">' . sprintf( $alt_text, esc_html( $instance['twitter_username'] ), 'Twitter' ) . '</span></a></li>';
  65. }
  66. if ( ! empty( $instance['instagram_username'] ) ) {
  67. $html .= '<li><a title="' . sprintf( $alt_text, esc_attr( $instance['instagram_username'] ), 'Instagram' ) . '" href="' . esc_url( 'https://instagram.com/' . $instance['instagram_username'] . '/' ) . '" class="genericon genericon-instagram" target="_blank"><span class="screen-reader-text">' . sprintf( $alt_text, esc_html( $instance['instagram_username'] ), 'Instagram' ) . '</span></a></li>';
  68. }
  69. if ( ! empty( $instance['pinterest_username'] ) ) {
  70. $html .= '<li><a title="' . sprintf( $alt_text, esc_attr( $instance['pinterest_username'] ), 'Pinterest' ) . '" href="' . esc_url( 'https://www.pinterest.com/' . $instance['pinterest_username'] . '/' ) . '" class="genericon genericon-pinterest-alt" target="_blank"><span class="screen-reader-text">' . sprintf( $alt_text, esc_html( $instance['pinterest_username'] ), 'Pinterest' ) . '</span></a></li>';
  71. }
  72. if ( ! empty( $instance['linkedin_username'] ) ) {
  73. $html .= '<li><a title="' . sprintf( $alt_text, esc_attr( $instance['linkedin_username'] ), 'LinkedIn' ) . '" href="' . esc_url( 'https://www.linkedin.com/in/' . $instance['linkedin_username'] . '/' ) . '" class="genericon genericon-linkedin-alt" target="_blank"><span class="screen-reader-text">' . sprintf( $alt_text, esc_html( $instance['linkedin_username'] ), 'LinkedIn' ) . '</span></a></li>';
  74. }
  75. if ( ! empty( $instance['github_username'] ) ) {
  76. $html .= '<li><a title="' . sprintf( $alt_text, esc_attr( $instance['github_username'] ), 'GitHub' ) . '" href="' . esc_url( 'https://github.com/' . $instance['github_username'] . '/' ) . '" class="genericon genericon-github" target="_blank"><span class="screen-reader-text">' . sprintf( $alt_text, esc_html( $instance['github_username'] ), 'GitHub' ) . '</span></a></li>';
  77. }
  78. if ( ! empty( $instance['youtube_username'] ) ) {
  79. $html .= '<li><a title="' . sprintf( $alt_text, esc_attr( $instance['youtube_username'] ), 'YouTube' ) . '" href="' . esc_url( 'https://www.youtube.com/channel/' . $instance['youtube_username'] ) . '" class="genericon genericon-youtube" target="_blank"><span class="screen-reader-text">' . sprintf( $alt_text, esc_html( $instance['youtube_username'] ), 'YouTube' ) . '</span></a></li>';
  80. }
  81. if ( ! empty( $instance['vimeo_username'] ) ) {
  82. $html .= '<li><a title="' . sprintf( $alt_text, esc_attr( $instance['vimeo_username'] ), 'Vimeo' ) . '" href="' . esc_url( 'https://vimeo.com/' . $instance['vimeo_username'] . '/' ) . '" class="genericon genericon-vimeo" target="_blank"><span class="screen-reader-text">' . sprintf( $alt_text, esc_html( $instance['vimeo_username'] ), 'Vimeo' ) . '</span></a></li>';
  83. }
  84. $html .= '</ul>';
  85. // after widget arguments
  86. $html .= $args['after_widget'];
  87. /**
  88. * Filters the Social Media Icons widget output.
  89. *
  90. * @since 3.6.0
  91. *
  92. * @param string $html Social Media Icons widget html output.
  93. */
  94. echo apply_filters( 'jetpack_social_media_icons_widget_output', $html );
  95. }
  96. // backend
  97. public function form( $instance ) {
  98. $instance = wp_parse_args( (array) $instance, $this->defaults );
  99. ?>
  100. <p>
  101. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'jetpack' ); ?></label>
  102. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
  103. </p>
  104. <p>
  105. <label for="<?php echo esc_attr( $this->get_field_id( 'facebook_username' ) ); ?>"><?php _e( 'Facebook username:', 'jetpack' ); ?></label>
  106. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'facebook_username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'facebook_username' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['facebook_username'] ); ?>" />
  107. </p>
  108. <p>
  109. <label for="<?php echo esc_attr( $this->get_field_id( 'twitter_username' ) ); ?>"><?php _e( 'Twitter username:', 'jetpack' ); ?></label>
  110. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'twitter_username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'twitter_username' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['twitter_username'] ); ?>" />
  111. </p>
  112. <p>
  113. <label for="<?php echo esc_attr( $this->get_field_id( 'instagram_username' ) ); ?>"><?php _e( 'Instagram username:', 'jetpack' ); ?></label>
  114. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'instagram_username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'instagram_username' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['instagram_username'] ); ?>" />
  115. </p>
  116. <p>
  117. <label for="<?php echo esc_attr( $this->get_field_id( 'pinterest_username' ) ); ?>"><?php _e( 'Pinterest username:', 'jetpack' ); ?></label>
  118. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'pinterest_username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'pinterest_username' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['pinterest_username'] ); ?>" />
  119. </p>
  120. <p>
  121. <label for="<?php echo esc_attr( $this->get_field_id( 'linkedin_username' ) ); ?>"><?php _e( 'LinkedIn username:', 'jetpack' ); ?></label>
  122. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'linkedin_username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'linkedin_username' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['linkedin_username'] ); ?>" />
  123. </p>
  124. <p>
  125. <label for="<?php echo esc_attr( $this->get_field_id( 'github_username' ) ); ?>"><?php _e( 'GitHub username:', 'jetpack' ); ?></label>
  126. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'github_username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'github_username' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['github_username'] ); ?>" />
  127. </p>
  128. <p>
  129. <label for="<?php echo esc_attr( $this->get_field_id( 'youtube_username' ) ); ?>"><?php _e( 'YouTube channel ID:', 'jetpack' ); ?></label>
  130. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'youtube_username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'youtube_username' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['youtube_username'] ); ?>" />
  131. </p>
  132. <p>
  133. <label for="<?php echo esc_attr( $this->get_field_id( 'vimeo_username' ) ); ?>"><?php _e( 'Vimeo username:', 'jetpack' ); ?></label>
  134. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'vimeo_username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'vimeo_username' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['vimeo_username'] ); ?>" />
  135. </p>
  136. <?php
  137. }
  138. // updating widget settings
  139. public function update( $new_instance, $old_instance ) {
  140. $instance = (array) $old_instance;
  141. foreach ( $new_instance as $field => $value ) {
  142. $instance[$field] = sanitize_text_field( $new_instance[$field] );
  143. }
  144. // Stats
  145. $stats = $instance;
  146. unset( $stats['title'] );
  147. $stats = array_filter( $stats );
  148. $stats = array_keys( $stats );
  149. $stats = array_map( array( $this, 'remove_username' ), $stats );
  150. foreach ( $stats as $val ) {
  151. /**
  152. * Fires for each Social Media account being saved in the Social Media Widget settings.
  153. *
  154. * @since 3.6.0
  155. *
  156. * @param string social-media-links-widget-svcs Type of action to track.
  157. * @param string $val Name of the Social Media account being saved.
  158. */
  159. do_action( 'jetpack_bump_stats_extras', 'social-media-links-widget-svcs', $val ) ;
  160. }
  161. return $instance;
  162. }
  163. // Remove username from value before to save stats
  164. public function remove_username( $val ) {
  165. return str_replace( '_username', '', $val );
  166. }
  167. } // class ends here
  168. // register and load the widget
  169. function wpcom_social_media_icons_widget_load_widget() {
  170. register_widget( 'wpcom_social_media_icons_widget' );
  171. }
  172. add_action( 'widgets_init', 'wpcom_social_media_icons_widget_load_widget' );