/** * Get share count for all non singular pages where $post is empty or a custom url is used E.g. category or blog list pages or for shortcodes * Uses transients * * @param string $url * * @returns integer $shares */ function mashsbGetNonPostShares($url) { global $mashsb_error; // Expiration $expiration = mashsb_get_expiration(); // Remove variables, parameters and trailingslash $url_clean = mashsb_sanitize_url($url); // Get any existing copy of our transient data and fill the cache if (mashsb_force_cache_refresh()) { // Its request limited if (mashsb_is_req_limited()) { $shares = get_transient('mashcount_' . md5($url_clean)); if (isset($shares) && is_numeric($shares)) { MASHSB()->logger->info('mashsbGetNonPostShares() get shares from get_transient. URL: ' . $url_clean . ' SHARES: ' . $shares); return $shares + getFakecount(); } else { return 0 + getFakecount(); // we need a result } } // Regenerate the data and save the transient // Get the share Object $mashsbSharesObj = mashsbGetShareObj($url_clean); // Get the share counts object $mashsbShareCounts = mashsbGetShareMethod($mashsbSharesObj); // Set the transient and return shares set_transient('mashcount_' . md5($url_clean), $mashsbShareCounts->total, $expiration); MASHSB()->logger->info('mashsbGetNonPostShares set_transient - shares:' . $mashsbShareCounts->total . ' url: ' . $url_clean); return $mashsbShareCounts->total + getFakecount(); } else { // Get shares from transient cache $shares = get_transient('mashcount_' . md5($url_clean)); if (isset($shares) && is_numeric($shares)) { MASHSB()->logger->info('mashsbGetNonPostShares() get shares from get_transient. URL: ' . $url_clean . ' SHARES: ' . $shares); return $shares + getFakecount(); } else { return 0 + getFakecount(); // we need a result } } }
/** * Get and store query from transient * * @param array $args * @return \WP_Query */ public function get_qry_from_cache($args) { $expiration = mashsb_get_expiration(); if (MASHSB_DEBUG) { delete_transient('mashwidget_' . md5(json_encode($args))); } if (false === ($qry = get_transient('mashwidget_' . md5(json_encode($args))))) { $wpq = new WP_Query($args); set_transient('mashwidget_' . md5(json_encode($args)), $wpq, $expiration); //wp_die( var_dump($wpq)); return $wpq; } else { //wp_die( var_dump($qry) ); return $qry; } }
/** * 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_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; } }