
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

if ( ! defined( 'PRESSO_CONST_SOCIAL_COUNTER_CACHE_EXPIRE' ) ) define( 'PRESSO_CONST_SOCIAL_COUNTER_CACHE_EXPIRE', 14400 ); // 60*60*4 = 4 Hrs cache

add_action( 'widgets_init', 'presso_widgets_init_social_counter' );
if ( ! function_exists( 'presso_widgets_init_social_counter' ) ) {
	function presso_widgets_init_social_counter() {
		register_widget( 'Presso_widget_social_counter' );
	}
}

if ( ! class_exists( 'Presso_widget_social_counter' ) ) {
	class Presso_widget_social_counter extends WP_Widget {
		private $default = array(
			'supertitle' => '',
			'title' => '',
			'subtitle' => '',
			'twitter' => '',
			'twitter_key' => '',
			'twitter_secret' => '',
			'facebook' => '',
			'facebook_app_id' => '',
			'facebook_app_secret' => '',
			'youtube' => '',
			'googleplus' => '',
			'google_api_key' => '',
			'vimeo' => '',
			'soundcloud' => '',
			'soundcloud_client_id' => '',
			'instagram' => '',
			'instagram_access_token' => '',
			'pinterest' => '',
			'vk' => '',
			'dribbble' => '',
			'is_saved' => false,
		);

		public function __construct() {
			// widget actual processes
			parent::__construct(
		 		'vw_widget_social_counter', // Base ID
				PRESSO_THEME_NAME.': Social Counter', // Name
				array( 'description' => esc_html__( 'Display social icon with counter', 'presso' ) ) // Args

			);
		}

		function widget( $args, $instance ) {
			extract($args);

			$instance['supertitle'] = apply_filters( 'wpml_translate_single_string', $instance['supertitle'], 'Widgets', $this->id.'_supertitle' );
			$instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
			$instance['subtitle'] = apply_filters( 'wpml_translate_single_string', $instance['subtitle'], 'Widgets', $this->id.'_subtitle' );

			$supertitle_html = '';
			if ( ! empty( $instance['supertitle'] ) ) {
				$supertitle_html = '<span class="vw-super-title">'. $instance['supertitle'] .'</span>';
			}

			$title_html = '';
			if ( ! empty( $instance['title'] ) ) {
				$title_html = $instance['title'];
			}

			$subtitle_html = '';
			if ( ! empty( $instance['subtitle'] ) ) {
				$subtitle_html = '<span class="vwspc-section-subtitle">'. $instance['subtitle'] .'</span>';
			}

			$twitter = sanitize_text_field( $instance['twitter'] );
			$twitter_key = sanitize_text_field( $instance['twitter_key'] );
			$twitter_secret = sanitize_text_field( $instance['twitter_secret'] );
			$facebook = sanitize_text_field( $instance['facebook'] );
			$facebook_app_id = sanitize_text_field( $instance['facebook_app_id'] );
			$facebook_app_secret = sanitize_text_field( $instance['facebook_app_secret'] );
			$youtube = sanitize_text_field( $instance['youtube'] );
			$googleplus = sanitize_text_field( $instance['googleplus'] );
			$google_api_key = sanitize_text_field( $instance['google_api_key'] );
			$vimeo = sanitize_text_field( $instance['vimeo'] );
			$soundcloud = sanitize_text_field( $instance['soundcloud'] );
			$soundcloud_client_id = sanitize_text_field( $instance['soundcloud_client_id'] );
			$instagram = sanitize_text_field( $instance['instagram'] );
			$instagram_access_token = sanitize_text_field( $instance['instagram_access_token'] );
			$pinterest = sanitize_text_field( $instance['pinterest'] );
			$vk = sanitize_text_field( $instance['vk'] );
			$dribbble = sanitize_text_field( $instance['dribbble'] );

			echo wp_kses_post( $before_widget );
			if ( $instance['title'] ) echo wp_kses_post( $supertitle_html . $before_title . $title_html . $after_title . $subtitle_html );

			?>
			<div class="vw-social-counter-list vw-social-counter-list--default-layout">
			<?php

			if ( $twitter ) {
				$twitter_count = presso_get_twitter_count( $twitter, $twitter_key, $twitter_secret );
				$this->render_social_item(
					'twitter',
					'icon-social-twitter',
					esc_html__( 'Follow our twitter', 'presso' ),
					$twitter_count['page_url'],
					$twitter_count['count'],
					esc_html__( 'Followers', 'presso' )
				);
			}

			if ( $facebook ) {
				$facebook_count = presso_get_facebook_count( $facebook, $facebook_app_id, $facebook_app_secret );
				$this->render_social_item(
					'facebook',
					'icon-social-facebook',
					esc_html__( 'Like our facebook', 'presso' ),
					$facebook_count['page_url'],
					$facebook_count['count'],
					esc_html__( 'Fans', 'presso' )
				);
			}

			if ( $youtube ) {
				$youtube_count = presso_get_youtube_count( $youtube, $google_api_key );
				$this->render_social_item(
					'youtube',
					'icon-social-youtube',
					esc_html__( 'Subscribe our youtube', 'presso' ),
					$youtube_count['page_url'],
					$youtube_count['count'],
					esc_html__( 'Members', 'presso' )
				);
			}

			if ( $googleplus ) {
				$googleplus_count = presso_get_googleplus_count( $googleplus, $google_api_key );
				$this->render_social_item(
					'googleplus',
					'icon-social-gplus',
					esc_html__( '+1 our page', 'presso' ),
					$googleplus_count['page_url'],
					$googleplus_count['count'],
					esc_html__( 'People', 'presso' )
				);
			}

			if ( $vimeo ) {
				$vimeo_count = presso_get_vimeo_count( $vimeo );
				$this->render_social_item(
					'vimeo',
					'icon-social-vimeo',
					esc_html__( 'Subscribe our page', 'presso' ),
					$vimeo_count['page_url'],
					$vimeo_count['count'],
					esc_html__( 'Members', 'presso' )
				);
			}

			if ( $soundcloud ) {
				$soundcloud_count = presso_get_soundcloud_count( $soundcloud, $soundcloud_client_id );
				$this->render_social_item(
					'soundcloud',
					'icon-social-soundcloud',
					esc_html__( 'Follow our Soundcloud', 'presso' ),
					$soundcloud_count['page_url'],
					$soundcloud_count['count'],
					esc_html__( 'Followers', 'presso' )
				);
			}

			if ( $instagram ) {
				$instagram_count = presso_get_instagram_count( $instagram, $instagram_access_token );
				$this->render_social_item(
					'instagram',
					'icon-social-instagram',
					esc_html__( 'Follow our instagram', 'presso' ),
					$instagram_count['page_url'],
					$instagram_count['count'],
					esc_html__( 'Followers', 'presso' )
				);
			}

			if ( $pinterest ) {
				$pinterest_count = presso_get_pinterest_count( $pinterest );
				$this->render_social_item(
					'pinterest',
					'icon-social-pinterest',
					esc_html__( 'Follow our Pinterest', 'presso' ),
					$pinterest_count['page_url'],
					$pinterest_count['count'],
					esc_html__( 'Followers', 'presso' )
				);
			}

			if ( $vk ) {
				$vk_count = presso_get_vk_count( $vk );
				$this->render_social_item(
					'vk',
					'icon-social-vk',
					esc_html__( 'Follow our vk', 'presso' ),
					$vk_count['page_url'],
					$vk_count['count'],
					esc_html__( 'Members', 'presso' )
				);
			}

			if ( $dribbble ) {
				$dribbble_count = presso_get_dribbble_count( $dribbble );
				$this->render_social_item(
					'dribbble',
					'icon-social-dribbble',
					esc_html__( 'Follow our Dribbble', 'presso' ),
					$dribbble_count['page_url'],
					$dribbble_count['count'],
					esc_html__( 'Followers', 'presso' )
				);
			}

			?>
			</div>
			<div class="clearfix"></div>
			<?php

			wp_reset_postdata();
			echo wp_kses_post( $after_widget );
		}

		function render_social_item( $slug, $icon, $title, $url, $counter, $unit ) {
			$counter_class = sprintf( 'vw-social-counter--%s', $slug );
			?>
				<a class="vw-social-counter <?php echo esc_attr( $counter_class );?>" href="<?php echo esc_attr( $url ); ?>" title="<?php esc_attr( $title ); ?>" target="_blank">
					<span class="vw-social-counter__icon"><i class="vw-icon <?php echo esc_attr( $icon );?>"></i></span>

					<?php if ( ! is_null( $counter ) ) : ?>
					<span class="vw-social-counter__counter">
						<span class="vw-social-counter__count"><?php echo presso_number_prefixes( $counter ); ?></span>
						<span class="vw-social-counter__unit"><?php echo esc_html( $unit ); ?></span>
					</span>
					<?php endif; ?>

					<span class="clearfix"></span>
				</a>
			<?php
		}

		function update( $new_instance, $old_instance ) {
			$instance = $old_instance;
			$new_instance = wp_parse_args( (array) $new_instance, $this->default );
			$instance['supertitle'] = sanitize_text_field( $new_instance['supertitle'] );
			$instance['title'] = sanitize_text_field( $new_instance['title'] );
			$instance['subtitle'] = sanitize_text_field( $new_instance['subtitle'] );
			$instance['twitter'] = sanitize_text_field( $new_instance['twitter'] );
			$instance['twitter_key'] = sanitize_text_field( $new_instance['twitter_key'] );
			$instance['twitter_secret'] = sanitize_text_field( $new_instance['twitter_secret'] );
			$instance['facebook'] = sanitize_text_field( $new_instance['facebook'] );
			$instance['facebook_app_id'] = sanitize_text_field( $new_instance['facebook_app_id'] );
			$instance['facebook_app_secret'] = sanitize_text_field( $new_instance['facebook_app_secret'] );
			$instance['youtube'] = sanitize_text_field( $new_instance['youtube'] );
			$instance['googleplus'] = sanitize_text_field( $new_instance['googleplus'] );
			$instance['google_api_key'] = sanitize_text_field( $new_instance['google_api_key'] );
			$instance['vimeo'] = sanitize_text_field( $new_instance['vimeo'] );
			$instance['soundcloud'] = sanitize_text_field( $new_instance['soundcloud'] );
			$instance['soundcloud_client_id'] = sanitize_text_field( $new_instance['soundcloud_client_id'] );
			$instance['instagram'] = sanitize_text_field( $new_instance['instagram'] );
			$instance['instagram_access_token'] = sanitize_text_field( $new_instance['instagram_access_token'] );
			$instance['pinterest'] = sanitize_text_field( $new_instance['pinterest'] );
			$instance['vk'] = sanitize_text_field( $new_instance['vk'] );
			$instance['dribbble'] = sanitize_text_field( $new_instance['dribbble'] );
			$instance['is_saved'] = true;

			delete_transient( 'vw_social_counter_twitter' );
			delete_transient( 'vw_social_counter_facebook' );
			delete_transient( 'vw_social_counter_youtube' );
			delete_transient( 'vw_social_counter_googleplus' );
			delete_transient( 'vw_social_counter_vimeo' );
			delete_transient( 'vw_social_counter_soundcloud' );
			delete_transient( 'vw_social_counter_instagram' );
			delete_transient( 'vw_social_counter_pinterest' );
			delete_transient( 'vw_social_counter_vk' );
			delete_transient( 'vw_social_counter_dribbble' );

			do_action( 'wpml_register_single_string', 'Widgets', $this->id.'_supertitle', $instance['supertitle'] );
			do_action( 'wpml_register_single_string', 'Widgets', $this->id.'_title', $instance['title'] );
			do_action( 'wpml_register_single_string', 'Widgets', $this->id.'_subtitle', $instance['subtitle'] );

			return $instance;
		}

		function form( $instance ) {
			$instance = wp_parse_args( (array) $instance, $this->default );

			$supertitle = $instance['supertitle'];
			$title = $instance['title'];
			$subtitle = $instance['subtitle'];
			$twitter = $instance['twitter'];
			$twitter_key = $instance['twitter_key'];
			$twitter_secret = $instance['twitter_secret'];
			$facebook = $instance['facebook'];
			$facebook_app_id = $instance['facebook_app_id'];
			$facebook_app_secret = $instance['facebook_app_secret'];
			$youtube = $instance['youtube'];
			$googleplus = $instance['googleplus'];
			$google_api_key = $instance['google_api_key'];
			$vimeo = $instance['vimeo'];
			$soundcloud = $instance['soundcloud'];
			$soundcloud_client_id = $instance['soundcloud_client_id'];
			$instagram = $instance['instagram'];
			$instagram_access_token = $instance['instagram_access_token'];
			$pinterest = $instance['pinterest'];
			$vk = $instance['vk'];
			$dribbble = $instance['dribbble'];
			?>

			<!-- supertitle -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('supertitle') ); ?>"><?php esc_html_e( 'Super-title:', 'presso' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('supertitle') ); ?>" name="<?php echo esc_attr( $this->get_field_name('supertitle') ); ?>" type="text" value="<?php echo esc_attr($supertitle); ?>" />
			</p>

			<!-- title -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('title') ); ?>"><?php esc_html_e( 'Title:', 'presso' ); ?></label>
				<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($title); ?>" />
			</p>

			<!-- subtitle -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('subtitle') ); ?>"><?php esc_html_e( 'Subtitle:', 'presso' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('subtitle') ); ?>" name="<?php echo esc_attr( $this->get_field_name('subtitle') ); ?>" type="text" value="<?php echo esc_attr($subtitle); ?>" />
			</p>

			<!-- twitter username -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('twitter') ); ?>"><?php esc_html_e( 'Twitter Username', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('twitter') ); ?>" name="<?php echo esc_attr( $this->get_field_name('twitter') ); ?>" type="text" value="<?php echo esc_attr($twitter); ?>" />
			</p>

			<!-- twitter consumer key -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('twitter_key') ); ?>"><?php esc_html_e( 'Twitter Consumer Key', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('twitter_key') ); ?>" name="<?php echo esc_attr( $this->get_field_name('twitter_key') ); ?>" type="text" value="<?php echo esc_attr($twitter_key); ?>" />
			</p>

			<!-- twitter consumer secret -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('twitter_secret') ); ?>"><?php esc_html_e( 'Twitter Consumer Secret', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('twitter_secret') ); ?>" name="<?php echo esc_attr( $this->get_field_name('twitter_secret') ); ?>" type="text" value="<?php echo esc_attr($twitter_secret); ?>" />
			</p>

			<!-- facebook id -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('facebook') ); ?>"><?php esc_html_e( 'Facebook Page ID', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('facebook') ); ?>" name="<?php echo esc_attr( $this->get_field_name('facebook') ); ?>" type="text" value="<?php echo esc_attr($facebook); ?>" />
				<p class="description">The value should be a page id like "<strong>80655071208</strong>".You can find the page id <a href="http://findmyfacebookid.com/" target="_blank">here</a>.</p>
			</p>

			<!-- facebook app id -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('facebook_app_id') ); ?>"><?php esc_html_e( 'Facebook App ID', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('facebook_app_id') ); ?>" name="<?php echo esc_attr( $this->get_field_name('facebook_app_id') ); ?>" type="text" value="<?php echo esc_attr($facebook_app_id); ?>" />
				<p class="description"></p>
			</p>

			<!-- facebook app secret -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('facebook_app_secret') ); ?>"><?php esc_html_e( 'Facebook App Secret', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('facebook_app_secret') ); ?>" name="<?php echo esc_attr( $this->get_field_name('facebook_app_secret') ); ?>" type="text" value="<?php echo esc_attr($facebook_app_secret); ?>" />
				<p class="description"></p>
			</p>

			<!-- youtube username -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('youtube') ); ?>"><?php esc_html_e( 'Youtube Username/Channel ID', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('youtube') ); ?>" name="<?php echo esc_attr( $this->get_field_name('youtube') ); ?>" type="text" value="<?php echo esc_attr($youtube); ?>" />
			</p>

			<!-- googleplus username -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('googleplus') ); ?>"><?php esc_html_e( 'Google+ Page Name', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('googleplus') ); ?>" name="<?php echo esc_attr( $this->get_field_name('googleplus') ); ?>" type="text" value="<?php echo esc_attr($googleplus); ?>" />
				<p class="description">The value should be a name or id like "<strong>+envato</strong>"" or "<strong>107285294994146126204</strong>"</p>
			</p>

			<!-- google api key -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('google_api_key') ); ?>"><?php esc_html_e( 'Google API Key', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('google_api_key') ); ?>" name="<?php echo esc_attr( $this->get_field_name('google_api_key') ); ?>" type="text" value="<?php echo esc_attr($google_api_key); ?>" />
				<p class="description"></p>
			</p>

			<!-- vimeo channel name -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('vimeo') ); ?>"><?php esc_html_e( 'Vimeo Channel Name/ID', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('vimeo') ); ?>" name="<?php echo esc_attr( $this->get_field_name('vimeo') ); ?>" type="text" value="<?php echo esc_attr($vimeo); ?>" />
				<p class="description">The value should be a name or id from your channel url like "<strong>staffpicks</strong>"" or "<strong>31259</strong>"</p>
			</p>

			<!-- soundcloud username -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('soundcloud') ); ?>"><?php esc_html_e( 'Soundcloud Username', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('soundcloud') ); ?>" name="<?php echo esc_attr( $this->get_field_name('soundcloud') ); ?>" type="text" value="<?php echo esc_attr($soundcloud); ?>" />
			</p>

			<!-- soundcloud client id -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('soundcloud_client_id') ); ?>"><?php esc_html_e( 'Soundcloud Client ID', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('soundcloud_client_id') ); ?>" name="<?php echo esc_attr( $this->get_field_name('soundcloud_client_id') ); ?>" type="text" value="<?php echo esc_attr($soundcloud_client_id); ?>" />
			</p>

			<!-- instagram -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('instagram') ); ?>"><?php esc_html_e( 'Instagram ID', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('instagram') ); ?>" name="<?php echo esc_attr( $this->get_field_name('instagram') ); ?>" type="text" value="<?php echo esc_attr($instagram); ?>" />
				<!-- <p class="description">This option was deprecated. Please use the <strong>instagram access token</strong> option instead.</p> -->
			</p>

			<!-- instagram access token -->
			<!--<p>
				<label for="<?php echo esc_attr( $this->get_field_id('instagram_access_token') ); ?>">Instagram Access Token:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('instagram_access_token') ); ?>" name="<?php echo esc_attr( $this->get_field_name('instagram_access_token') ); ?>" type="text" value="<?php echo esc_attr($instagram_access_token); ?>" />
				<p class="description">You can find an instagram access token to connect your instagram at <a href="http://instagram.pixelunion.net/" target="_blank">here</a>.</p>
			</p>-->

			<!-- pinterest username -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('pinterest') ); ?>"><?php esc_html_e( 'Pinterest Username', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('pinterest') ); ?>" name="<?php echo esc_attr( $this->get_field_name('pinterest') ); ?>" type="text" value="<?php echo esc_attr($pinterest); ?>" />
			</p>

			<!-- vk group id -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('vk') ); ?>"><?php esc_html_e( 'VK Group ID', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('vk') ); ?>" name="<?php echo esc_attr( $this->get_field_name('vk') ); ?>" type="text" value="<?php echo esc_attr($vk); ?>" />
			</p>

			<!-- dribbble username -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('dribbble') ); ?>"><?php esc_html_e( 'Dribbble Username', 'presso' ); ?>:</label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('dribbble') ); ?>" name="<?php echo esc_attr( $this->get_field_name('dribbble') ); ?>" type="text" value="<?php echo esc_attr($dribbble); ?>" />
			</p>


			<?php
		}
	}
}

/* -----------------------------------------------------------------------------
 * Get Counter Functions
 * -------------------------------------------------------------------------- */

if ( ! function_exists( 'presso_save_social_counter' ) ) {
	function presso_save_social_counter( $key, $counter ) {
		if ( is_null( $counter['count'] ) ){
			// When getting counter failed.
			$saved_counter = get_option( $key, array() );

			if ( ! empty( $saved_counter['count'] ) && $saved_counter['page_url'] == $counter['page_url'] ) {
				// Restore previous counter
				$counter['count'] = $saved_counter['count'];
			} else {
				$counter['count'] = 0;
			}

		} else {
			// Save the counter when getting counter successfully.
			update_option( $key, $counter );
		}

		set_transient( $key, $counter, PRESSO_CONST_SOCIAL_COUNTER_CACHE_EXPIRE );

		return $counter;
	}
}

if ( ! function_exists( 'presso_get_twitter_count' ) ) {
	function presso_get_twitter_count( $twitter_id, $consumer_key, $consumer_secret ) {
		$counter_key = 'vw_social_counter_twitter';
		$consumer_key = apply_filters( 'vw_filter_twitter_consumer_key', $consumer_key );
		$consumer_secret = apply_filters( 'vw_filter_twitter_consumer_secret', $consumer_secret );

		/**
		 * Check for cached version
		 */
		$twitter = get_transient( $counter_key );
		if ( $twitter !== false ) return $twitter;

		/**
		 * Getting counter
		 */
		$twitter = array();
		$twitter['page_url'] = "https://www.twitter.com/$twitter_id";
		$twitter['count'] = null;

		if( $twitter_id && $consumer_key && $consumer_secret ) {
			$token = get_option( 'vw_twitter_token' );
			if( ! $token ) {
				// preparing credentials
				$credentials = $consumer_key . ':' . $consumer_secret;
				$toSend = base64_encode( $credentials );

				// http post arguments
				$args = array(
					'method' => 'POST',
					'httpversion' => '1.1',
					'blocking' => true,
					'sslverify' => false,
					'headers' => array(
						'Authorization' => 'Basic ' . $toSend,
						'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8'
					),
					'body' => array( 'grant_type' => 'client_credentials' )
				);

				$response = wp_remote_post('https://api.twitter.com/oauth2/token', $args);

				$keys = json_decode( wp_remote_retrieve_body( $response ) );

				if( $keys ) {
					// saving token to wp_options table
					update_option( 'vw_twitter_token', $keys->access_token );
					$token = $keys->access_token;
				}
			}

			// we have bearer token wether we obtained it from API or from options
			$args = array(
				'httpversion' => '1.1',
				'blocking' => true,
				'sslverify' => false,
				'headers' => array(
					'Authorization' => "Bearer $token"
				)
			);
		
			$api_url = "https://api.twitter.com/1.1/users/show.json?screen_name=$twitter_id";
			$response = wp_remote_get( $api_url, $args );

			if ( ! is_wp_error( $response ) ) {
				$twitter_reply = json_decode( wp_remote_retrieve_body( $response ) );
				if ( isset( $twitter_reply->followers_count ) ) {
					$twitter['count'] = $twitter_reply->followers_count;
				}
			} else {
				// Delete token for getting a new token on the next request
				delete_option( 'vw_twitter_token' );
			}
		}

		/**
		 * Save counter
		 */
		return presso_save_social_counter( $counter_key, $twitter );
	}
}

if ( ! function_exists( 'presso_get_facebook_count' ) ) {
	function presso_get_facebook_count( $page_id, $facebook_app_id, $facebook_app_secret ) {
		$counter_key = 'vw_social_counter_facebook';
		$facebook_app_id = apply_filters( 'vw_filter_facebook_app_id', $facebook_app_id );
		$facebook_app_secret = apply_filters( 'vw_filter_facebook_app_secret', $facebook_app_secret );

		/**
		 * Check for cached version
		 */
		$facebook = get_transient( $counter_key );
		if ( $facebook !== false ) return $facebook;

		/**
		 * Getting counter
		 */
		$facebook = array();
		$facebook['page_url'] = "https://www.facebook.com/".$page_id;
		$facebook['count'] = null;

		if( $page_id ) {
			$token = get_option( 'vw_facebook_token' );
			if( ! $token ) {
				// we have bearer token wether we obtained it from API or from options
				$args = array(
					'httpversion' => '1.1',
					'blocking' => true,
					// 'sslverify' => false,
					'headers' => array(),
				);

				$response = wp_remote_get( 'https://graph.facebook.com/oauth/access_token?client_id='.urlencode( $facebook_app_id ).'&client_secret='.urlencode( $facebook_app_secret ).'&grant_type=client_credentials', $args );

				$body = wp_remote_retrieve_body( $response );

				if( strpos( $body, 'access_token=' ) !== false ) {
					// saving token to wp_options table
					$key = str_replace( 'access_token=', '', $body );
					update_option( 'vw_facebook_token', $key );
					$token = $key;
				} else {
					// Delete token for getting a new token on the next request
					delete_option( 'vw_facebook_token' );
				}
			}

			// we have bearer token wether we obtained it from API or from options
			$args = array(
				'httpversion' => '1.1',
				'blocking' => true,
				// 'sslverify' => false,
				'headers' => array(),
			);

			add_filter( 'https_ssl_verify', '__return_false' );
			$api_url = 'https://graph.facebook.com/v2.3/'.$page_id.'?access_token='.$token;
			$response = wp_remote_get( $api_url, $args );

			if ( ! is_wp_error( $response ) ) {
				$facebook_reply = json_decode( wp_remote_retrieve_body( $response ) );
				if ( isset( $facebook_reply->likes ) ) {
					$facebook['count'] = $facebook_reply->likes;
				}
			} else {
				// Delete token for getting a new token on the next request
				delete_option( 'vw_facebook_token' );
			}
		}

		/**
		 * Save counter
		 */
		return presso_save_social_counter( $counter_key, $facebook );
	}
}

if ( ! function_exists( 'presso_get_instagram_count' ) ) {
	function presso_get_instagram_count( $username, $access_token ) {
		$counter_key = 'vw_social_counter_instagram';
		$access_token = apply_filters( 'vw_filter_instagram_access_token', $access_token );

		/**
		 * Check for cached version
		 */
		$instagram = get_transient( $counter_key );
		if ( $instagram !== false ) return $instagram;

		/**
		 * Getting counter
		 */
		$api_url = 'https://instagram.com/'.$username.'/';
		$instagram = array();
		$instagram['page_url'] = $api_url;
		$instagram['count'] = null;
		
		$raw_page_content = presso_get_subscriber_counter( $api_url ); 

		if ( ! is_wp_error( $raw_page_content ) ) {
			if ( preg_match( '|"followed_by":[ ]*{"count":[ ]*([0-9]+)|', $raw_page_content, $match ) ) {
				$instagram['count'] = $match[1];
			}
		}

		/**
		 * Save counter
		 */
		return presso_save_social_counter( $counter_key, $instagram );
	}
}

if ( ! function_exists( 'presso_get_youtube_count' ) ) {
	function presso_get_youtube_count( $channel_id, $google_api_key ) {
		$counter_key = 'vw_social_counter_youtube';
		$google_api_key = apply_filters( 'vw_filter_google_api_key', $google_api_key );

		/**
		 * Check for cached version
		 */
		$youtube = get_transient( $counter_key );
		if ( $youtube !== false ) return $youtube;

		/**
		 * Getting counter
		 */
		$youtube = array();
		$youtube['page_url'] = sprintf( 'https://www.youtube.com/channel/%1$s/', $channel_id );
		$youtube['count'] = null;
		$api_url = sprintf( 'https://www.googleapis.com/youtube/v3/channels?part=statistics&id=%1$s&key=%2$s', $channel_id, $google_api_key );
		$alt_api_url = sprintf( 'https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=%1$s&key=%2$s', $channel_id, $google_api_key );

		$data = presso_get_subscriber_counter($api_url);

		if ( ! is_wp_error( $data ) ) {
			$json = json_decode( $data );

			if ( isset( $json->items[0]->statistics->subscriberCount ) ) {
				$youtube['count'] = $json->items[0]->statistics->subscriberCount;
			} else {

				/* Try again with alternative url for getting statistics from user id */
				$youtube['page_url'] = sprintf( 'https://www.youtube.com/user/%1$s/', $channel_id );
				$data = presso_get_subscriber_counter( $alt_api_url );
				$json = json_decode( $data );
				if ( isset( $json->items[0]->statistics->subscriberCount ) ) {
					$youtube['count'] = $json->items[0]->statistics->subscriberCount;
				}
			}
		}

		/**
		 * Save counter
		 */
		return presso_save_social_counter( $counter_key, $youtube );
	}
}

if ( ! function_exists( 'presso_get_googleplus_count' ) ) {
	function presso_get_googleplus_count( $username, $google_api_key ) {
		$counter_key = 'vw_social_counter_googleplus';
		$google_api_key = apply_filters( 'vw_filter_google_api_key', $google_api_key );

		/**
		 * Check for cached version
		 */
		$googleplus = get_transient( $counter_key );
		if ( $googleplus !== false ) return $googleplus;

		/**
		 * Getting counter
		 */
		$googleplus = array();
		$googleplus['page_url'] = 'https://plus.google.com/'.$username;
		$googleplus['count'] = null;
		

		if ( preg_match( '/[^0-9]/', $username ) && strpos( $username, '+' ) !== 0 ) {
			// Reformat username
			$username = '+'.$username;
		}
		$api_url = sprintf( 'https://www.googleapis.com/plus/v1/people/%1$s?key=%2$s', $username, $google_api_key );
		
		$data = presso_get_subscriber_counter($api_url); 

		if ( ! is_wp_error( $data ) ) {
			$json = json_decode( $data );

			if ( isset( $json->url ) ) {
				$googleplus['page_url'] = $json->url;
			}

			if ( isset( $json->circledByCount ) ) {
				$googleplus['count'] = $json->circledByCount;

			} elseif ( isset( $json->plusOneCount ) ) {
				$googleplus['count'] = $json->plusOneCount;
				
			}
		}

		/**
		 * Save counter
		 */
		presso_save_social_counter( $counter_key, $googleplus );

		return $googleplus;
	}
}

if ( ! function_exists( 'presso_get_vimeo_count' ) ) {
	function presso_get_vimeo_count( $channel_name ) {
		$counter_key = 'vw_social_counter_vimeo';

		/**
		 * Check for cached version
		 */
		$vimeo = get_transient( $counter_key );
		if ( $vimeo !== false ) return $vimeo;

		/**
		 * Getting counter
		 */
		$vimeo = array();
		$vimeo['page_url'] = 'https://vimeo.com/channels/'.$channel_name;
		$vimeo['count'] = null;
		$api_url = 'http://vimeo.com/api/v2/channel/'.$channel_name.'/info.json';
		
		$data = presso_get_subscriber_counter( $api_url ); 

		if ( ! is_wp_error( $data ) ) {
			$json = json_decode( $data );

			if ( isset( $json->url ) ) {
				$vimeo['page_url'] = $json->url;
			}

			if ( isset( $json->total_subscribers ) ) {
				$vimeo['count'] = $json->total_subscribers;
			}
		}

		/**
		 * Save counter
		 */
		return presso_save_social_counter( $counter_key, $vimeo );
	}
}

if ( ! function_exists( 'presso_get_soundcloud_count' ) ) {
	function presso_get_soundcloud_count( $user, $client_id ) {
		$counter_key = 'vw_social_counter_soundcloud';
		$client_id = apply_filters( 'vw_filter_soundcloud_client_id', $client_id );

		/**
		 * Check for cached version
		 */
		$soundcloud = get_transient( $counter_key );
		if ( $soundcloud !== false ) return $soundcloud;

		/**
		 * Getting counter
		 */
		$soundcloud['page_url'] = 'https://soundcloud.com/'.$user;
		$soundcloud['count'] = null;
		$api_url = 'http://api.soundcloud.com/users/' . $user . '.json?client_id=' . $client_id;
		
		$data = presso_get_subscriber_counter( $api_url ); 

		if ( ! is_wp_error( $data ) ) {
			$json = json_decode( $data );

			if ( isset( $json->permalink_url ) ) {
				$soundcloud['page_url'] = $json->permalink_url;
			}

			if ( isset( $json->followers_count ) ) {
				$soundcloud['count'] = $json->followers_count;
			}
		}

		/**
		 * Save counter
		 */
		return presso_save_social_counter( $counter_key, $soundcloud );
	}
}

if ( ! function_exists( 'presso_get_pinterest_count' ) ) {
	function presso_get_pinterest_count( $username ) {
		$counter_key = 'vw_social_counter_pinterest';

		/**
		 * Check for cached version
		 */
		$pinterest = get_transient( $counter_key );
		if ( $pinterest !== false ) return $pinterest;

		/**
		 * Getting counter
		 */
		$api_url = 'https://www.pinterest.com/'.$username.'/';
		$pinterest['page_url'] = $api_url;
		$pinterest['count'] = null;
		add_filter( 'https_ssl_verify', '__return_false' );
		$data = presso_get_subscriber_counter( $api_url ); 
		if ( ! is_wp_error( $data ) ) {
			$doc = new DOMDocument();
			@$doc->loadHTML( $data );
			$metas = $doc->getElementsByTagName( 'meta' );
			for ( $i = 0; $i < $metas->length; $i++ ){
				$meta = $metas->item( $i );
				if( $meta->getAttribute('name') == 'pinterestapp:followers' ){
					$pinterest['count'] = $meta->getAttribute( 'content' );
					break;
				}
			}
		}

		/**
		 * Save counter
		 */
		return presso_save_social_counter( $counter_key, $pinterest );
	}
}

if ( ! function_exists( 'presso_get_vk_count' ) ) {
	function presso_get_vk_count( $group_id ) {
		$counter_key = 'vw_social_counter_vk';

		/**
		 * Check for cached version
		 */
		$pinterest = get_transient( $counter_key );
		if ( $pinterest !== false ) return $pinterest;

		/**
		 * Getting counter
		 */
		$vk['page_url'] = 'http://vk.com/'.$group_id;
		$vk['count'] = null;
		$api_url = 'http://api.vk.com/method/groups.getById?gid='.$group_id.'&fields=members_count';
		
		$data = presso_get_subscriber_counter( $api_url ); 

		if ( ! is_wp_error( $data ) ) {
			$json = json_decode( $data );

			if ( isset( $json->response[0]->members_count ) ) {
				$vk['count'] = $json->response[0]->members_count;
			}
		}

		/**
		 * Save counter
		 */
		return presso_save_social_counter( $counter_key, $vk );
	}
}

if ( ! function_exists( 'presso_get_dribbble_count' ) ) {
	function presso_get_dribbble_count( $username ) {
		$counter_key = 'vw_social_counter_dribbble';

		/**
		 * Check for cached version
		 */
		$pinterest = get_transient( $counter_key );
		if ( $pinterest !== false ) return $pinterest;

		/**
		 * Getting counter
		 */
		$dribbble['page_url'] = 'https://dribbble.com/'.$username;
		$dribbble['count'] = null;
		$api_url = 'http://api.dribbble.com/'.$username;
		
		$data = presso_get_subscriber_counter( $api_url ); 

		if ( ! is_wp_error( $data ) ) {
			$json = json_decode( $data );

			if ( isset( $json->url ) ) {
				$dribbble['page_url'] = $json->url;
			}

			if ( isset( $json->followers_count ) ) {
				$dribbble['count'] = $json->followers_count;
			}
		}

		/**
		 * Save counter
		 */
		return presso_save_social_counter( $counter_key, $dribbble );
	}
}

if ( ! function_exists( 'presso_get_subscriber_counter' ) ) {
	function presso_get_subscriber_counter( $api_url ) {
		$args = array(
			'httpversion' => '1.1',
			'blocking' => true,
		);

		$response = wp_remote_get( $api_url, $args );
		if ( ! is_wp_error( $response ) ) {
			return wp_remote_retrieve_body( $response );
		}
	}
}