Exemplo n.º 1
0
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);
    }
}
Exemplo n.º 2
0
function mashsb_get_shortened_url($url)
{
    global $mashsb_options;
    // Return empty
    if (empty($url)) {
        return "";
    }
    // Use native WP Shortlinks
    if ($mashsb_options['mashsu_methods'] === 'wpshortlinks') {
        return wp_get_shortlink();
    }
    // If is_singular post store shorturl in custom post fields
    // and return it from there so user has the power to change values
    // on his own from the post edit page and the custom fields editor
    if (is_singular()) {
        return mashsb_get_shorturl_singular($url);
    }
    // Force cache rebuild
    if (mashsb_force_cache_refresh()) {
        MASHSB()->logger->info('mashsb_get_shorturl() -> refresh cache');
        // bitly shortlink
        if (isset($mashsb_options['mashsu_methods']) && $mashsb_options['mashsu_methods'] === 'bitly') {
            $shorturl = mashsb_get_bitly_link($url);
            MASHSB()->logger->info('create shorturl: ' . $url . ' ' . $shorturl);
        }
        // Google shortlink
        if (isset($mashsb_options['mashsu_methods']) && $mashsb_options['mashsu_methods'] === 'google') {
            $shorturl = mashsb_get_google_link($url);
        }
        // Get expiration time
        $expiration = mashsb_get_expiration();
        set_transient('mash_url_' . md5($url), $shorturl, $expiration);
    } else {
        $shorturl = get_transient('mash_url_' . md5($url));
    }
    if (!empty($shorturl)) {
        return $shorturl;
    } else {
        return $url;
    }
}