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); } }