public function widget($args, $instance)
 {
     // extract widget options
     extract($args);
     $title = apply_filters('widget_title', $instance['title']);
     $count = $instance['count'];
     $excerpt_length = !empty($instance['excerpt_length']) ? esc_attr($instance['excerpt_length']) : 0;
     $title_length = !empty($instance['title_length']) ? esc_attr($instance['title_length']) : 70;
     $image_size = !empty($instance['image_size']) ? esc_attr($instance['image_size']) : 0;
     $showShares = $instance['showShares'];
     $countLabel = $instance['countLabel'];
     $period = !empty($instance['period']) ? $instance['period'] : '7';
     echo '<!-- MashShare Most Popular Widget //-->';
     echo $before_widget;
     // Display the widget
     // Check if title is set
     if ($title) {
         echo $before_title . $title . $after_title;
     }
     // Check if text is set
     $args = array('posts_per_page' => $count, 'post_type' => 'post', 'post_status' => 'publish', 'meta_key' => 'mashsb_shares', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'date_query' => array(array('after' => $period . ' days ago', 'inclusive' => true)));
     //$wpq = new WP_Query( $args );
     $wpq = $this->get_qry_from_cache($args);
     //var_dump($wpq);
     if ($wpq->have_posts()) {
         echo '<ul class="mashsb-share-widget">';
         while ($wpq->have_posts()) {
             $wpq->the_post();
             $postID = get_the_ID();
             $image_url = wp_get_attachment_url(get_post_thumbnail_id($postID));
             if (!empty($image_url)) {
                 $css = 'background-image: url(' . wp_get_attachment_url(get_post_thumbnail_id($postID)) . ');background-size: cover;background-repeat: no-repeat;background-position: 50% 50%;width:' . $image_size . 'px;height:' . $image_size . 'px;';
                 $image = !empty($image_size) ? '<div class="mashsb-widget-img" style="' . $css . '"><a class="mashsb-widget-link" href="' . get_the_permalink() . '" style="display:block;width:' . $image_size . 'px;height:' . $image_size . 'px;">&nbsp</a></div>' : '';
             } else {
                 $css = 'display:block;width:' . $image_size . 'px;height:' . $image_size . 'px;';
                 $image = !empty($image_size) ? '<div class="mashsb-widget-img" style="' . $css . '"><a class="mashsb-widget-link" href="' . get_the_permalink() . '">&nbsp</a></div>' : '';
             }
             $title_result = '<div class="mashsb-widget-post-title"><a class="mashsb-widget-link" href="' . get_the_permalink() . '">' . $this->limit_title(get_the_title(), $title_length) . '</a></div>';
             $excerpt = !empty($excerpt_length) ? '<div class="mashsb-excerpt">' . $this->limit_excerpt(get_the_excerpt($postID), $excerpt_length) . '</div>' : '';
             if ($showShares === 'true') {
                 $shares = get_post_meta($postID, 'mashsb_shares', true) + getFakecount();
                 echo '<li>' . $image . $title_result . $excerpt . ' <span class="mashicon-share">' . roundshares($shares) . ' ' . $countLabel . '</span></li>';
             } else {
                 echo '<li>' . $image . $title_result . $excerpt . '</li>';
             }
         }
         echo '</ul>';
     }
     wp_reset_postdata();
     echo $after_widget;
     echo '<!-- MashShare Most Popular Widget End //-->';
 }
function getSharedcount($url)
{
    global $wpdb, $mashsb_options, $post;
    if (is_null($post)) {
        return apply_filters('filter_get_sharedcount', 0);
    }
    isset($mashsb_options['mashsharer_cache']) ? $cacheexpire = $mashsb_options['mashsharer_cache'] : ($cacheexpire = 300);
    /* make sure 300sec is default value */
    $cacheexpire < 300 ? $cacheexpire = 300 : $cacheexpire;
    if (isset($mashsb_options['disable_cache'])) {
        $cacheexpire = 5;
    }
    /* Bypass next lines and return share count for pages with empty $post object
         share count for pages where $post is empty. E.g. category or blog list pages
         Otherwise share counts are requested with every page load 
       *      */
    /*if (is_null($post)) {
      	return apply_filters('filter_get_sharedcount', mashsbGetNonPostShares($url, $cacheexpire));
      }*/
    $mashsbNextUpdate = (int) $cacheexpire;
    $mashsbLastUpdated = get_post_meta($post->ID, 'mashsb_timestamp', true);
    if (empty($mashsbLastUpdated)) {
        $mashsbCheckUpdate = true;
        $mashsbLastUpdated = 0;
    }
    if ($mashsbLastUpdated + $mashsbNextUpdate <= time()) {
        mashdebug()->info("First Update - Frequency: " . $mashsbNextUpdate . " Next update: " . date('Y-m-d H:i:s', $mashsbLastUpdated + $mashsbNextUpdate) . " last updated: " . date('Y-m-d H:i:s', $mashsbLastUpdated) . " Current time: " . date('Y-m-d H:i:s', time()));
        // Get the share Object
        $mashsbSharesObj = mashsbGetShareObj($url);
        // Get the share counts
        $mashsbShareCounts = mashsbGetShareMethod($mashsbSharesObj);
        //$mashsbShareCounts = new stdClass(); // USE THIS FOR DEBUGGING
        //$mashsbShareCounts->total = 13; // USE THIS FOR DEBUGGING
        $mashsbStoredDBMeta = get_post_meta($post->ID, 'mashsb_shares', true);
        // Write timestamp
        update_post_meta($post->ID, 'mashsb_timestamp', time());
        /* Update post_meta only when API is requested and
         * API share count is greater than real fresh requested share count ->
         * ### This meas there is an error in the API (Failure or hammering any limits, e.g. X-Rate-Limit) ###
         */
        if ($mashsbShareCounts->total >= $mashsbStoredDBMeta) {
            update_post_meta($post->ID, 'mashsb_shares', $mashsbShareCounts->total);
            update_post_meta($post->ID, 'mashsb_jsonshares', json_encode($mashsbShareCounts));
            mashdebug()->info("updated database with share count: " . $mashsbShareCounts->total);
            /* return counts from getAllCounts() after DB update */
            return apply_filters('filter_get_sharedcount', $mashsbShareCounts->total + getFakecount());
        }
        /* return previous counts from DB Cache | this happens when API has a hiccup and does not return any results as expected */
        return apply_filters('filter_get_sharedcount', $mashsbStoredDBMeta + getFakecount());
    } else {
        /* return counts from post_meta plus fake count | This is regular cached result */
        $cachedCountsMeta = get_post_meta($post->ID, 'mashsb_shares', true);
        $cachedCounts = $cachedCountsMeta + getFakecount();
        mashdebug()->info("Cached result - Frequency: " . $mashsbNextUpdate . " Next update: " . date('Y-m-d H:i:s', $mashsbLastUpdated + $mashsbNextUpdate) . " last updated: " . date('Y-m-d H:i:s', $mashsbLastUpdated) . " Current time: " . date('Y-m-d H:i:s', time()));
        return apply_filters('filter_get_sharedcount', $cachedCounts);
    }
}
function getSharedcount($url)
{
    global $mashsb_options, $post, $mashsb_sharecount, $mashsb_error;
    // todo test a global share count var if it reduces the amount of requests
    // Return global share count variable to prevent multiple execution
    if (is_array($mashsb_sharecount) && array_key_exists($url, $mashsb_sharecount) && !empty($mashsb_sharecount[$url]) && !mashsb_is_cache_refresh()) {
        return $mashsb_sharecount[$url] + getFakecount();
    }
    // Remove mashsb-refresh query parameter
    $url = mashsb_sanitize_url($url);
    /*
     * Deactivate share count on:
     * - 404 pages
     * - search page
     * - empty url
     * - disabled permalinks
     * - disabled share count setting
     * - deprecated: admin pages (we need to remove this for themes which are using a bad infinite scroll implementation where is_admin() is always true)
     */
    if (is_404() || is_search() || empty($url) || !mashsb_is_enabled_permalinks() || isset($mashsb_options['disable_sharecount'])) {
        return apply_filters('filter_get_sharedcount', 0);
    }
    /* 
    * Return share count on non singular pages when url is defined
      Possible: Category, blog list pages, non singular() pages. This store the shares in transients with mashsbGetNonPostShares();
    */
    if (!empty($url) && is_null($post)) {
        return apply_filters('filter_get_sharedcount', mashsbGetNonPostShares($url));
    }
    /*
     * Refresh Cache
     */
    if (mashsb_force_cache_refresh() && is_singular()) {
        // Its request limited
        if (mashsb_is_req_limited()) {
            return get_post_meta($post->ID, 'mashsb_shares', true) + getFakecount();
        }
        // free some memory
        unset($mashsb_sharecount[$url]);
        // Write timestamp (Use this on top of this condition. If this is not on top following return statements will be skipped and ignored - possible bug?)
        update_post_meta($post->ID, 'mashsb_timestamp', time());
        MASHSB()->logger->info('Refresh Cache: Update Timestamp: ' . time());
        // Get the share Object
        $mashsbSharesObj = mashsbGetShareObj($url);
        // Get the share count Method
        $mashsbShareCounts = mashsbGetShareMethod($mashsbSharesObj);
        // Get stored share count
        $mashsbStoredShareCount = get_post_meta($post->ID, 'mashsb_shares', true);
        // Create global sharecount
        $mashsb_sharecount = array($url => $mashsbShareCounts->total);
        /*
         * Update post_meta only when API is requested and
         * API share count is greater than real fresh requested share count ->
         */
        //wp_die('error' . $mashsbShareCounts->error);
        if ($mashsbShareCounts->total >= $mashsbStoredShareCount) {
            update_post_meta($post->ID, 'mashsb_shares', $mashsbShareCounts->total);
            update_post_meta($post->ID, 'mashsb_jsonshares', json_encode($mashsbShareCounts));
            MASHSB()->logger->info("Refresh Cache: Update database with share count: " . $mashsbShareCounts->total);
            /* return counts from getAllCounts() after DB update */
            return apply_filters('filter_get_sharedcount', $mashsbShareCounts->total + getFakecount());
        }
        /* return previous counts from DB Cache | this happens when API has a hiccup and does not return any results as expected */
        return apply_filters('filter_get_sharedcount', $mashsbStoredShareCount + getFakecount());
    } else {
        // Return cached results
        $cachedCountsMeta = get_post_meta($post->ID, 'mashsb_shares', true);
        $cachedCounts = $cachedCountsMeta + getFakecount();
        MASHSB()->logger->info('Cached Results: ' . $cachedCounts . ' url:' . $url);
        return apply_filters('filter_get_sharedcount', $cachedCounts);
    }
}