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