?AlkantarClanX12
| Current Path : /home/r/a/n/rankinh/leblogauto/wp-content/plugins/rubik-extension/widgets/ |
| Current File : /home/r/a/n/rankinh/leblogauto/wp-content/plugins/rubik-extension/widgets/widget_social_counter.php |
<?php
/**
* Add function to widgets_init that'll load our widget.
*/
add_action('widgets_init','bk_register_social_counters_widget');
function number_format_short( $n ) {
if ($n > 0 && $n < 1000) {
// 1 - 999
$n_format = floor($n);
$suffix = '';
} else if ($n >= 1000 && $n < 1000000) {
// 1k-999k
$n_format = floor($n / 1000).'.'.round(($n % 1000)/100);
$suffix = 'K+';
} else if ($n >= 1000000 && $n < 1000000000) {
// 1m-999m
$n_format = floor($n / 1000000).'.'.round(($n % 1000000)/100000);
$suffix = 'M+';
} else if ($n >= 1000000000 && $n < 1000000000000) {
// 1b-999b
$n_format = floor($n / 1000000000).'.'.round(($n % 1000000000)/100000000);
$suffix = 'B+';
} else if ($n >= 1000000000000) {
// 1t+
$n_format = floor($n / 1000000000000).'.'.round(($n % 1000000000000)/1000000000000);
$suffix = 'T+';
}else {
$n_format = '';
$suffix = '';
}
$fn = $n_format . $suffix;
return !empty($fn) ? $fn : '';
}
function bk_extract_one_number( $str ) {
return intval( preg_replace( '/[^0-9]+/', '', $str ), 10 );
}
function bk_fetch_social_count($response, $filter){
$response = wp_remote_retrieve_body( $response );
//print_r($response);
if ( ! empty( $response ) && $response !== false ) {
foreach ( $filter as $key => $value ) {
$key = explode( '_', $key );
$key = $key[0];
if ( $key == 'start' ) {
$key = false;
} else if ( $key == 'end' ) {
$key = true;
}
$key = (bool) $key;
$index = strpos( $response, $value );
if ( $index === false ) {
return false;
}
if ( $key ) {
$response = substr( $response, 0, $index );
} else {
$response = substr( $response, $index + strlen( $value ) );
}
}
if ( strlen( $response ) > 100 ) {
return false;
}
$count = bk_extract_one_number( $response );
if ( ! is_numeric( $count ) || strlen( number_format( $count ) ) > 15 ) {
return false;
}
$count = intval( $count );
return $count;
} else {
return false;
}
}
function bk_register_social_counters_widget() {
register_widget('bk_social_counter');
}
/**
* This class handles everything that needs to be handled with the widget:
* the settings, form, display, and update. Nice!
*
*/
class bk_social_counter extends WP_Widget {
private $connection;
private $consumer_key;
private $consumer_secret;
private $access_token;
private $access_token_secret;
/**
* Widget setup.
*/
function __construct() {
/* Widget settings. */
$widget_ops = array('classname' => 'widget-social-counter','description' => esc_html__('Displays social counters', 'rubik'));
/* Create the widget. */
parent::__construct('bk_social_counter',esc_html__('BK: Social Counters', 'rubik'),$widget_ops);
}
/**
* display the widget on the screen.
*/
function widget( $args, $instance ) {
extract( $args );
$hours = 1;
//user settings
$title = apply_filters('widget_title', $instance['title'] );
$layout = esc_attr($instance['layout']);
if($layout == '') {
$layout = '1';
}else if($layout == '8') {
$layout = '6 layout-8';
}else if($layout == '9') {
$layout = '7 layout-9';
}else if($layout == '10') {
$layout = '6 layout-10';
}else if($layout == '11') {
$layout = '7 layout-11';
}else if($layout == '12') {
$layout = '6 layout-12';
}else if($layout == '13') {
$layout = '6 layout-10 layout-13';
}
$social_widget_style = esc_attr($instance['social_widget_style']);
if($social_widget_style == '') {
$social_widget_style = 'light';
}
$rubik_youtube_channel = isset($instance['youtube_channel']) ? esc_attr($instance['youtube_channel']) : "";
$rubik_youtube_api = isset($instance['youtube_api']) ? esc_attr($instance['youtube_api']) : "";
$rubik_pinterest_url = isset($instance['pinterest_url']) ? esc_attr($instance['pinterest_url']) : "";
$rubik_rss_url = isset($instance['rubik_rss_url']) ? esc_attr($instance['rubik_rss_url']) : "";
$rubik_facebook_url = isset($instance['facebook_url']) ? esc_attr($instance['facebook_url']) : "";
$rubik_twitter_url = isset($instance['twitter_url']) ? esc_attr($instance['twitter_url']) : "";
$rubik_twitter_text = isset($instance['twitter_text']) ? esc_attr($instance['twitter_text']) : "";
echo ($before_widget);
if ( $title )
echo ($before_title . esc_html($title) . $after_title);
//twitter
// if (isset($rubik_twitter_url)&&(strlen($rubik_twitter_url) != 0)){
// $followers = rubik_core::rubik_read_count_social($rubik_twitter_url, 'twitter');
// $transient__name = 'social_json_transient_twitter';
// $cache = get_transient( $transient__name );
// if ( $cache === false || $cache == '' ) {
// if ( ! empty( $followers ) ) {
// update_option('rubik_twitter_followers', $followers);
// }
// set_transient( $transient__name, $followers, 60 * 60 * $hours );
// }
// }
//facebook
if (isset($rubik_facebook_url)&&(strlen($rubik_facebook_url) != 0)){
$transient__name = 'social_json_transient_facebook';
$followers = rubik_core::rubik_read_count_social($rubik_facebook_url, 'facebook');
if ( ! empty( $followers ) ) {
update_option('rubik_facebook_followers', $followers);
update_option('rubik_facebook_link', esc_url($rubik_facebook_url));
}
set_transient( $transient__name, $followers, 60 * 60 * $hours );
}
if(isset($rubik_youtube_channel)&&(strlen($rubik_youtube_channel) != 0)){
$transient__name = 'social_json_transient_youtube';
$cache = get_transient( $transient__name );
if($rubik_youtube_api != ''):
$followers = rubik_core::rubik_read_count_social($rubik_youtube_channel, 'youtube', $rubik_youtube_api);
else:
$followers = 0;
endif;
if ( ! empty( $followers ) ) {
update_option('rubik_youtube_cache_time', time() + 600);
update_option('rubik_youtube_subscribers', $followers);
}
set_transient( $transient__name, $followers, 60 * 60 * $hours );
}
if(isset($rubik_pinterest_url)&&(strlen($rubik_pinterest_url) != 0)){
$followers = rubik_core::rubik_read_count_social(esc_url($rubik_pinterest_url), 'pinterest');
$transient__name = 'social_json_transient_pinterest';
$cache = get_transient( $transient__name );
if ( $cache === false || $cache == '' ) {
if ( ! empty( $followers ) ) {
update_option('rubik_pinterest_cache_time', time() + 600);
update_option('rubik_pinterest_followers', $followers);
update_option('rubik_pinterest_link', esc_url($rubik_pinterest_url));
}
set_transient( $transient__name, $followers, 60 * 60 * $hours );
}
}
?>
<div class="social-counter-wrap clearfix layout-<?php echo $layout;?> <?php echo $social_widget_style;?>">
<ul class="clearfix">
<?php if (isset($rubik_twitter_url)&&(strlen($rubik_twitter_url) != 0)){ ?>
<li class="twitter clear-fix">
<a target="_blank" href="<?php echo esc_url($rubik_twitter_url);?>">
<div class="social-icon"><i class="fa fa-twitter"></i></div>
<div class="data">
<?php if (!empty($rubik_twitter_text)): ?>
<div class="counter"><?php echo "".$rubik_twitter_text;?></div>
<?php endif; ?>
</div>
</a>
</li> <!-- /twitter -->
<?php } ?>
<?php if (isset($rubik_facebook_url) && (strlen($rubik_facebook_url) != 0)){ ?>
<li class="facebook clear-fix">
<a target="_blank" href="<?php echo get_option('rubik_facebook_link'); ?>">
<div class="social-icon"><i class="fa fa-facebook"></i></div>
<div class="data">
<div class="counter"><?php echo get_option('rubik_facebook_followers'); ?></div>
<div class="text"><?php esc_html_e( 'Likes', 'rubik');?></div>
</div>
</a>
</li><!-- /facebook -->
<?php } ?>
<?php if (isset($rubik_youtube_channel)&&(strlen($rubik_youtube_channel) != 0)){ ?>
<li class="youtube clear-fix">
<a target="_blank" href="https://www.youtube.com/channel/<?php echo esc_attr($rubik_youtube_channel);?>">
<div class="social-icon"><i class="fa fa-youtube"></i></div>
<div class="data">
<div class="counter"><?php echo get_option('rubik_youtube_subscribers'); ?></div>
<div class="text"><?php esc_html_e( 'Subscribers', 'rubik'); ?></div>
</div>
</a>
</li>
<?php } ?>
<?php if (isset($rubik_pinterest_url)&&(strlen($rubik_pinterest_url) != 0)){ ?>
<li class="pinterest clear-fix">
<a target="_blank" href="<?php echo get_option('rubik_pinterest_link'); ?>">
<div class="social-icon">
<i class="fa fa-pinterest"></i>
</div>
<div class="data">
<div class="counter"><?php echo get_option('rubik_pinterest_followers'); ?></div>
<div class="text"><?php esc_html_e( 'Followers', 'rubik'); ?></div>
</div>
</a>
</li>
<?php } ?>
<?php if (isset($rubik_rss_url)&&(strlen($rubik_rss_url) != 0)){ ?>
<li class="rss clear-fix">
<a target="_blank" href="<?php echo esc_url($rubik_rss_url); ?>">
<div class="social-icon"><i class="fa fa-rss"></i></div>
<div class="data">
<div class="subscribe"><?php esc_html_e( 'Subscribe', 'rubik'); ?></div>
<div class="text"><?php esc_html_e( 'RSS Feeds', 'rubik'); ?></div>
</div>
</a>
</li>
<?php } ?>
</ul>
</div><!-- /wrap -->
<?php
echo ($after_widget);
}
/**
* update widget settings
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['layout'] = $new_instance['layout'];
$instance['social_widget_style'] = $new_instance['social_widget_style'];
$instance['facebook_url'] = $new_instance['facebook_url'];
$instance['youtube_channel'] = $new_instance['youtube_channel'];
$instance['youtube_api'] = $new_instance['youtube_api'];
$instance['pinterest_url'] = $new_instance['pinterest_url'];
$instance['rubik_rss_url'] = $new_instance['rubik_rss_url'];
$instance['twitter_url'] = $new_instance['twitter_url'];
$instance['twitter_text'] = $new_instance['twitter_text'];
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
* Make use of the get_field_id() and get_field_name() function
* when creating your form elements. This handles the confusing stuff.
*/
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array(
'title' => '',
'layout' => '1',
'social_widget_style' => 'light',
'youtube_channel' => '',
'youtube_api' => '',
'pinterest_url' => '',
'twitter_url' => '',
'twitter_text' => '',
'facebook_url' => '',
'rubik_rss_url' => '',
);
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<!-- Title: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><strong><?php esc_html_e('Title:', 'rubik');?></strong></label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr($instance['title']); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('layout'); ?>"><strong><?php esc_html_e('Select a layout: ', 'rubik'); ?></strong></label>
<select id="<?php echo $this->get_field_id('layout'); ?>" name="<?php echo $this->get_field_name('layout'); ?>" style="width:100%;">
<option value='1' <?php if ('1' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('1', 'rubik');?></option>
<option value='2' <?php if ('2' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('2', 'rubik');?></option>
<option value='3' <?php if ('3' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('3', 'rubik');?></option>
<option value='4' <?php if ('4' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('4', 'rubik');?></option>
<option value='5' <?php if ('5' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('5', 'rubik');?></option>
<option value='6' <?php if ('6' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('6', 'rubik');?></option>
<option value='7' <?php if ('7' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('7', 'rubik');?></option>
<option value='8' <?php if ('8' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('8', 'rubik');?></option>
<option value='9' <?php if ('9' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('9', 'rubik');?></option>
<option value='10' <?php if ('10' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('10', 'rubik');?></option>
<option value='11' <?php if ('11' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('11', 'rubik');?></option>
<option value='12' <?php if ('12' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('12', 'rubik');?></option>
<option value='13' <?php if ('13' == $instance['layout']) echo 'selected="selected"'; ?>><?php esc_html_e('13', 'rubik');?></option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'social_widget_style' ); ?>"><strong><?php esc_html_e('Style:', 'rubik');?></strong></label>
<select id="<?php echo $this->get_field_id('social_widget_style'); ?>" name="<?php echo $this->get_field_name('social_widget_style'); ?>" style="width:100%;">
<option value='light' <?php if ('light' == $instance['social_widget_style']) echo 'selected="selected"'; ?>><?php esc_html_e('Light Style', 'rubik');?></option>
<option value='dark' <?php if ('dark' == $instance['social_widget_style']) echo 'selected="selected"'; ?>><?php esc_html_e('Dark Style', 'rubik');?></option>
</select>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id( 'facebook_url' )); ?>"><strong><?php esc_html_e('Facebook URL:', 'rubik'); ?></strong></label>
<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id('facebook_url')); ?>" name="<?php echo esc_attr($this->get_field_name('facebook_url')); ?>" value="<?php if( !empty($instance['facebook_url']) ) echo esc_attr($instance['facebook_url']); ?>" />
<i><?php esc_attr_e('eg. https://www.facebook.com/envato','rubik') ?></i>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id( 'youtube_channel' )); ?>"><strong><?php esc_html_e('Youtube Channel ID:', 'rubik'); ?></strong></label>
<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id('youtube_channel')); ?>" name="<?php echo esc_attr($this->get_field_name('youtube_channel')); ?>" value="<?php if( !empty($instance['youtube_channel']) ) echo esc_attr($instance['youtube_channel']); ?>" />
<i><a href="https://commentpicker.com/youtube-channel-id.php"><?php esc_attr_e('Get your Channel ID by this tool: https://commentpicker.com/youtube-channel-id.php','rubik') ?></a></i>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id( 'youtube_api' )); ?>"><strong><?php esc_html_e('Youtube API Key:', 'rubik'); ?></strong></label>
<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id('youtube_api')); ?>" name="<?php echo esc_attr($this->get_field_name('youtube_api')); ?>" value="<?php if( !empty($instance['youtube_api']) ) echo esc_attr($instance['youtube_api']); ?>" />
<i><a href="https://console.developers.google.com/"><?php esc_attr_e('Get the API Key here: https://console.developers.google.com/','rubik') ?></a></i>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id( 'pinterest_url' )); ?>"><strong><?php esc_html_e('Pinterest URL:', 'rubik'); ?></strong></label>
<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id('pinterest_url')); ?>" name="<?php echo esc_attr($this->get_field_name('pinterest_url')); ?>" value="<?php if( !empty($instance['pinterest_url']) ) echo esc_attr($instance['pinterest_url']); ?>" />
<i><?php esc_attr_e('eg. https://pinterest.com/envato','rubik') ?></i>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'rubik_rss_url' ); ?>"><strong><?php esc_html_e( 'RSS URL', 'rubik');?></strong></label>
<input type="text" id="<?php echo $this->get_field_id( 'rubik_rss_url' ); ?>" name="<?php echo $this->get_field_name( 'rubik_rss_url' ); ?>" value="<?php echo esc_url($instance['rubik_rss_url']); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id( 'twitter_url' )); ?>"><strong><?php esc_html_e('Twitter URL:', 'rubik'); ?></strong></label>
<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id('twitter_url')); ?>" name="<?php echo esc_attr($this->get_field_name('twitter_url')); ?>" value="<?php if( !empty($instance['twitter_url']) ) echo esc_attr($instance['twitter_url']); ?>" />
<i><?php esc_attr_e('eg. https://www.twitter.com/envato','rubik') ?></i>
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id( 'twitter_text' )); ?>"><strong><?php esc_html_e('Twitter Text:', 'rubik'); ?></strong></label>
<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id('twitter_text')); ?>" name="<?php echo esc_attr($this->get_field_name('twitter_text')); ?>" value="<?php if( !empty($instance['twitter_text']) ) echo esc_attr($instance['twitter_text']); ?>" />
<i><?php esc_attr_e('eg. envato','rubik') ?></i>
</p>
<?php
}
} //end class