<?php add_action('widgets_init', 'serpentsoft_socialCounter_widget_init');

function serpentsoft_socialCounter_widget_init() {
    register_widget('serpentsoft_socialCounter_widget');
}

class serpentsoft_socialCounter_widget extends WP_Widget {

    function serpentsoft_socialCounter_widget() {
        $widget_ops = array('classname' => 'w-social-counter', 'description' => '');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-social-counter');
        $this->WP_Widget('id-widget-social-counter', theme_name . ' - Social Counter Box', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        extract($args);
        $title = apply_filters('widget_title', $instance['title']);

        $rss_feed = $instance['rss_feed'];

        $fb_fans = $instance['facebook_fans'];
        $twt_followers = $instance['twitter_followers'];
        $yt_subscribers = $instance['youtube_subscribers'];
        $vm_subscribers = $instance['vimeo_subscribers'];
        $drb_followers = $instance['dribbble_followers'];

        echo $before_widget;
        if ($title) :
            echo $before_title;
            echo $title;
            echo $after_title;
        endif; ?>
        <div class="social-counter-metro">
            <ul class="clearfix">

            <!-- Facebook Fans -->
            <?php if ($fb_fans): ?>
                <?php $fb_pageName = serpentsoft_get_data('serpentsoft_facebookAPI_pageName'); ?>
                    <?php if (!empty($fb_pageName)): ?>
                        <li class="fb-wrp">
                            <div class="btn-count">
                                <div class="social-counter-metro mask">
                                    <span>Like</span>
                                    <a href="http://www.facebook.com/<?php echo $fb_pageName; ?>" target="_blank" class="more"></a>
                                </div>
                                <div class="facebook">
                                    <i class="facebook-sicon-48-white"></i>
                                    <span><?php echo serpentsoft_counter_facebook($fb_pageName) ?></span>
                                    <span class="fans"><?php _e('Fans', 'serpentsoft'); ?></span>
                                </div>
                            </div>
                        </li>
                    <?php endif; ?>
            <?php endif; ?>

        <!-- Twitter Followers -->
        <?php if ($twt_followers): ?>
            <?php $screenName = serpentsoft_get_data('serpentsoft_twitterAPI_screenName');
            $consumerKey = serpentsoft_get_data('serpentsoft_twitterAPI_consumerkey');
            $consumerSecret = serpentsoft_get_data('serpentsoft_twitterAPI_consumersecret'); ?>

            <?php if (!empty($screenName) && !empty($consumerKey) && !empty($consumerSecret)): ?>
                        <li class="tr-wrp">
                            <div class="btn-count">
                                <div class="social-counter-metro mask">
                                    <span>Follow</span>
                                    <a href="http://twitter.com/<?php echo $screenName; ?>" target="_blank" class="more"></a>
                                </div>
                                <div class="twitter">
                                    <i class="twitter-sicon-48-white"></i>
                                    <span><?php echo serpentsoft_counter_twitter($screenName, $consumerKey, $consumerSecret); ?></span>
                                    <span class="fans"><?php _e('Followers', 'serpentsoft'); ?></span>
                                </div>
                            </div>
                        </li>
            <?php endif; ?>
        <?php endif; ?>

        <!-- Youtube Subscribers -->
        <?php if ($yt_subscribers): ?>
            <?php $yt_channel = serpentsoft_get_data('serpentsoft_youtubeAPI_channel'); ?>

            <?php if (!empty($yt_channel)): ?>
                        <li class="yt-wrp">
                            <div class="btn-count">
                                <div class="social-counter-metro mask">
                                    <span>Subscribe</span>
                                    <a href="http://www.youtube.com/<?php echo $yt_channel; ?>" target="_blank" class="more"></a>
                                </div>

                                <div class="youtube">
                                    <i class="youtube-sicon-48-white"></i>
                                    <span><?php echo serpentsoft_counter_youtube($yt_channel); ?></span>
                                    <span class="fans"><?php _e('Subscribers', 'serpentsoft'); ?></span>
                                </div>
                            </div>
                        </li>
            <?php endif; ?>
        <?php endif; ?>

        <!-- Vimeo Subscribers -->
        <?php if ($vm_subscribers): ?>
            <?php $vm_channel = serpentsoft_get_data('serpentsoft_vimeoAPI_channel'); ?>

            <?php if (!empty($vm_channel)): ?>
                        <li class="vm-wrp">
                            <div class="btn-count">
                                <div class="social-counter-metro mask">
                                    <span>Subscribe</span>
                                    <a href="http://vimeo.com/channels/<?php echo $vm_channel; ?>" target="_blank" class="more"></a>
                                </div>

                                <div class="vimeo">
                                    <i class="vimeo-sicon-48-white"></i>
                                    <span><?php echo serpentsoft_counter_vimeo($vm_channel); ?></span>
                                    <span class="fans"><?php _e('Subscribers', 'serpentsoft'); ?></span>
                                </div>
                            </div>
                        </li>
            <?php endif; ?>
        <?php endif; ?>

        <!-- Dribbble Followers -->
        <?php if ($drb_followers): ?>
            <?php $drb_pageName = serpentsoft_get_data('serpentsoft_dribbbleAPI_pageName'); ?>

            <?php if (!empty($drb_pageName)): ?>
                        <li class="drb-wrp">
                            <div class="btn-count">
                                <div class="social-counter-metro mask">
                                    <span>Follow</span>
                                    <a href="http://dribbble.com/<?php echo $drb_pageName; ?>" target="_blank" class="more"></a>
                                </div>

                                <div class="dribbble">
                                    <i class="dribbble-sicon-48-white"></i>
                                    <span><?php echo serpentsoft_counter_dribbble($drb_pageName); ?></span>
                                    <span class="fans"><?php _e('Followers', 'serpentsoft'); ?></span>
                                </div>
                            </div>
                        </li>
            <?php endif; ?>
        <?php endif; ?>

        <!-- RSS Feed -->
        <?php if ($rss_feed): ?>
                    <li class="rs-wrp">
                        <div class="btn-count">
                            <div class="social-counter-metro mask">
                                <span>Subscribe</span>
                                <a href="<?php echo $rss_feed; ?>" target="_blank" class="more"></a>
                            </div>

                            <div class="rss">
                                <i class="rss-sicon-48-white"></i>
                                <span>Subscribe</span>
                                <span class="fans"><?php _e('To RSS Feed', 'serpentsoft'); ?></span>
                            </div>
                        </div>
                    </li>
        <?php endif; ?>

            </ul>
        </div>
        <?php
        echo $after_widget;
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['rss_feed'] = $new_instance['rss_feed'];

        $instance['facebook_fans'] = strip_tags($new_instance['facebook_fans']);
        $instance['twitter_followers'] = strip_tags($new_instance['twitter_followers']);
        $instance['youtube_subscribers'] = strip_tags($new_instance['youtube_subscribers']);
        $instance['vimeo_subscribers'] = strip_tags($new_instance['vimeo_subscribers']);
        $instance['dribbble_followers'] = strip_tags($new_instance['dribbble_followers']);

        delete_transient('cached_FacebookFans');
        delete_transient('cached_TwitterFollowers');
        delete_transient('cached_YoutubeSubscribers');
        delete_transient('cached_DribbbleFollowers');
        delete_transient('cached_VimeoSubscribers');

        return $instance;
    }

    function form($instance) {
        $defaults = array('title' => __('Social Counter Box', 'serpentsoft'),
            'rss_feed' => '',
            'facebook_fans' => '',
            'twitter_followers' => '',
            'youtube_subscribers' => '',
            'vimeo_subscribers' => '',
            'googleplus_followers' => '',
            'dribbble_followers' => '',
        );
        $instance = wp_parse_args((array) $instance, $defaults);
        ?>

        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'serpentsoft'); ?>: </label>
            <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>"
                   value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('rss_feed'); ?>"><?php _e('RSS Feed URL', 'serpentsoft'); ?>: </label>
            <input id="<?php echo $this->get_field_id('rss_feed'); ?>" name="<?php echo $this->get_field_name('rss_feed'); ?>"
                   value="<?php echo $instance['rss_feed']; ?>" class="widefat" type="text" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('facebook_fans'); ?>"><?php _e('View Facebook Fans?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('facebook_fans'); ?>" name="<?php echo $this->get_field_name('facebook_fans'); ?>"
                   value="true" <?php if ($instance['facebook_fans']) {
            echo 'checked="checked"';
        } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('twitter_followers'); ?>"><?php _e('View Twitter Followers?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('twitter_followers'); ?>" name="<?php echo $this->get_field_name('twitter_followers'); ?>"
                   value="true" <?php if ($instance['twitter_followers']) {
            echo 'checked="checked"';
        } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('youtube_subscribers'); ?>"><?php _e('View Youtube Subscribers?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('youtube_subscribers'); ?>" name="<?php echo $this->get_field_name('youtube_subscribers'); ?>"
                   value="true" <?php if ($instance['youtube_subscribers']) {
            echo 'checked="checked"';
        } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('vimeo_subscribers'); ?>"><?php _e('View Vimeo Subscribers?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('vimeo_subscribers'); ?>" name="<?php echo $this->get_field_name('vimeo_subscribers'); ?>"
                   value="true" <?php if ($instance['vimeo_subscribers']) {
            echo 'checked="checked"';
        } ?> type="checkbox" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('dribbble_followers'); ?>"><?php _e('View Dribbble Followers?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('dribbble_followers'); ?>" name="<?php echo $this->get_field_name('dribbble_followers'); ?>"
                   value="true" <?php if ($instance['dribbble_followers']) {
            echo 'checked="checked"';
        } ?> type="checkbox" />
        </p>

    <?php } } ?>