/** * Check if cache time is expired and post must be refreshed * * @global array $post * @return boolean */ function mashsb_is_cache_refresh() { global $post, $mashsb_options; // Debug mode or cache activated if (MASHSB_DEBUG || isset($mashsb_options['disable_cache'])) { MASHSB()->logger->info('mashsb_is_cache_refresh: MASHSB_DEBUG - refresh Cache'); return true; } // if it's a crawl deactivate cache if (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider/i', $_SERVER['HTTP_USER_AGENT'])) { return false; } /* * Deactivate share count on: * - 404 pages * - search page * - empty url * - disabled permalinks * - admin pages * Exit here to save cpu time */ if (is_404() || is_search() || is_admin() || !mashsb_is_enabled_permalinks()) { return false; } // New cache on singular pages // // Refreshing cache on blog posts like categories will lead // to high load and multiple API requests so we only check // the main url on these other pages if (is_singular()) { // last updated timestamp $last_updated = get_post_meta($post->ID, 'mashsb_timestamp', true); if (!empty($last_updated)) { MASHSB()->logger->info('mashsb_is_cache_refresh - is_singular() url: ' . get_permalink($post->ID) . ' : last updated:' . date('Y-m-d H:i:s', $last_updated)); } } else { if (mashsb_get_main_url()) { // Get transient timeout and calculate last update time $url = mashsb_get_main_url(); $transient = '_transient_timeout_mashcount_' . md5(mashsb_get_main_url()); $last_updated = get_option($transient) - mashsb_get_expiration(); if (!empty($last_updated)) { MASHSB()->logger->info('mashsb_is_cache_refresh() mashsb_get_main_url() url: ' . $url . ' last updated:' . date('Y-m-d H:i:s', $last_updated)); } } else { // No valid URL so do not refresh cache MASHSB()->logger->info('mashsb_is_cache_refresh: No valid URL - do not refresh cache'); return false; } } // No timestamp so let's create cache for the first time if (empty($last_updated)) { MASHSB()->logger->info('mashsb_is_cache_refresh: No Timestamp. Refresh Cache'); return true; } // The caching expiration $expiration = mashsb_get_expiration(); $next_update = $last_updated + $expiration; MASHSB()->logger->info('mashsb_is_cache_refresh. Next update ' . date('Y-m-d H:i:s', $next_update) . ' current time: ' . date('Y-m-d H:i:s', time())); // Refresh Cache when last update plus expiration is older than current time if ($last_updated + $expiration <= time()) { MASHSB()->logger->info('mashsb_is_cache_refresh: Refresh Cache!'); return true; } }
function mashsb_get_url() { global $post; if (isset($post->ID)) { // The permalink for singular pages! // Do not check here for is_singular() (like e.g. the sharebar addon does.) // Need to check for post id because on category and archiv pages // we want the pageID within the loop instead the first appearing one. $url = mashsb_sanitize_url(get_permalink($post->ID)); } else { // The main URL $url = mashsb_get_main_url(); } return apply_filters('mashsb_get_url', $url); }