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