<?php
// Get Twitter Follower count as plain text
add_option('pp_twitter_followers','0','','yes');
add_option('pp_twitter_api_timer',mktime(),'','yes');

function pp_twitter_followers() {
	$twittercount = 0;
    
    if ( $twittercount == 0 OR get_option(SHORTNAME.'_twitter_api_timer') < (mktime() - 3600) ) {
        // EDIT your Twitter user name here:
        $twitter_id = get_option(SHORTNAME.'_twitter_username');
        $followers = curl("http://api.twitter.com/1/users/show.xml?screen_name=" . $twitter_id);

        try {
            $xml = new SimpleXmlElement($followers, LIBXML_NOCDATA);
            if ($xml) {
                $twittercount = (string) $xml->followers_count;
                //var_dump('twitter');
                
                if($twittercount > 0)
				{
                	update_option('pp_twitter_followers', $twittercount);
                	update_option('pp_twitter_api_timer', mktime());
                }
                else
                {
                	$twittercount = get_option('pp_twitter_followers');
                }
            }
        } catch (Exception $e) { }
    }
    else
    {
    	$twittercount = get_option('pp_twitter_followers');
    }
    if ( $twittercount != '0' ) { echo number_format($twittercount); }
    else { echo 0; }
}

// Get Feedburner RSS Subscriber count as plain text
add_option('pp_feeds_count','0','','yes');
add_option('pp_feeds_api_timer',mktime(),'','yes');

function pp_feeds_count() {
    $rsscount = 0;

    if ( $rsscount == 0 OR get_option('pp_feeds_api_timer') < (mktime() - 3600) ) {
    //if ( true ) {
        // EDIT your Feedburner feed name here:
        $fb_id = get_option('feedburner-id');
        $subscribers = curl("http://feeds.feedburner.com/" . $fb_id);
        
        try {
            $xml = new SimpleXmlElement($subscribers, LIBXML_NOCDATA);
            //var_dump($xml);

            if ($xml) {
                $rsscount = (string) $xml->feed->entry['circulation'];

                if($rsscount > 0)
				{
                	update_option('pp_feeds_count', $rsscount);
                	update_option('pp_feeds_api_timer', mktime());
                }
                else
                {
                	$rsscount = get_option('pp_feeds_count');
                }
            }
        } catch (Exception $e) { }
    }
    else
    {
    	$rsscount = get_option('pp_feeds_count');
    }
    
    //Echo the count if we got it
    if($rsscount == 'N/A' || $rsscount == '0') { echo 0; }
    else { echo number_format($rsscount); }
}

// Get Twitter fan count as plain text
add_option('pp_facebook_fans','0','','yes');
add_option('pp_facebook_api_timer',mktime(),'','yes');

function pp_facebook_fans() {
	$fancount = 0;
    
    if ( $fancount == 0 OR get_option(SHORTNAME.'pp_facebook_api_timer') < (mktime() - 3600) ) {
        $page_id = get_option(SHORTNAME.'_facebook_username');
        $page_graph_data = curl("http://graph.facebook.com/".$page_id);
        try {
            $graph_obj = json_decode($page_graph_data);
			$count_fan = $graph_obj->likes;
            
			if($count_fan > 0)
			{
            	update_option('pp_facebook_fans', $count_fan);
            	update_option('pp_facebook_api_timer', mktime());
            }
            else
            {
            	$count_fan = get_option('pp_facebook_fans');
            }
        } catch (Exception $e) { }
    }
    else
    {
    	$count_fan = get_option('pp_twitter_followers');
    }
    if ( $count_fan != '0' ) { echo number_format($count_fan); }
    else { echo 0; }
}

function wpapi_pagination($pages = '', $range = 4)
{
 $showitems = ($range * 2)+1;
 
 global $paged;
 if(empty($paged)) $paged = 1;
 
 if($pages == '')
 {
 global $wp_query;
 $pages = $wp_query->max_num_pages;
 if(!$pages)
 {
 $pages = 1;
 }
 }
 
 if(1 != $pages)
 {
 echo "<div class=\"pagination\">";
 if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; اولین</a>";
 if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; قبلی</a>";
 
 for ($i=1; $i <= $pages; $i++)
 {
 if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
 {
 echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
 }
 }
 
 if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
 if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>آخرین &raquo;</a>";
 echo "</div>\n";
 }
}


// Clean up Shortcodes
function pp_clean_shortcodes($content){   
    $array = array (
        '<p>[' => '[', 
        ']</p>' => ']', 
        ']<br />' => ']'
    );
    $content = strtr($content, $array);
    return $content;
}
add_filter('the_content', 'pp_clean_shortcodes');


//Make widget support shortcode
add_filter('widget_text', 'do_shortcode');

//Add custom tag could font size
function pp_tag_cloud_filter($args = array()) {
   $args['smallest'] = 11;
   $args['largest'] = 11;
   $args['unit'] = 'px';
   return $args;
}

add_filter('widget_tag_cloud_args', 'pp_tag_cloud_filter', 90);

if (isset($_GET['activated']) && $_GET['activated']){
	global $wpdb;
	
	// Run default settings
	include_once(get_template_directory() . "/default_settings.php");
	create_ratinglogs_table();
	
    wp_redirect(admin_url("themes.php?page=admin-action.lib.php&activate=true"));
}
?>