/** * Load Scripts * * Enqueues the required scripts. * * @since 1.0 * @global $mashsb_options * @global $post * @return void * @param string $hook Page hook */ function mashsb_load_scripts($hook) { global $mashsb_options, $post, $mashsb_sharecount; if (!apply_filters('mashsb_load_scripts', mashsbGetActiveStatus(), $hook)) { mashdebug()->info("mashsb_load_script not active"); return; } $url = mashsb_get_url(); $title = urlencode(html_entity_decode(the_title_attribute('echo=0'), ENT_COMPAT, 'UTF-8')); $title = str_replace('#', '%23', $title); $titleclean = esc_html($title); $image = ""; $desc = ""; if (isset($post->ID)) { $image = mashsb_get_image($post->ID); $desc = mashsb_get_excerpt_by_id($post->ID); } // Rest API Not used any longer //$restapi = mashsb_allow_rest_api() ? "1" : "0"; /* Load hashshags */ $hashtag = !empty($mashsb_options['mashsharer_hashtag']) ? $mashsb_options['mashsharer_hashtag'] : ''; $js_dir = MASHSB_PLUGIN_URL . 'assets/js/'; // Use minified libraries if Mashshare debug mode is turned off $suffix = mashsbIsDebugMode() ? '' : '.min'; isset($mashsb_options['load_scripts_footer']) ? $in_footer = true : ($in_footer = false); wp_enqueue_script('mashsb', $js_dir . 'mashsb' . $suffix . '.js', array('jquery'), MASHSB_VERSION, $in_footer); //wp_enqueue_script( 'element-queries', $js_dir . 'ElementQueries' . '.js', array('jquery'), MASHSB_VERSION, $in_footer ); //wp_enqueue_script( 'resize-sensor', $js_dir . 'ResizeSensor' . '.js', array('jquery'), MASHSB_VERSION, $in_footer ); !isset($mashsb_options['disable_sharecount']) ? $shareresult = getSharedcount($url) : ($shareresult = 0); wp_localize_script('mashsb', 'mashsb', array('shares' => $shareresult, 'round_shares' => isset($mashsb_options['mashsharer_round']), 'animate_shares' => isset($mashsb_options['animate_shares']) && is_singular() ? 1 : 0, 'dynamic_buttons' => isset($mashsb_options['dynamic_button_resize']) ? 1 : 0, 'share_url' => $url, 'title' => $titleclean, 'image' => $image, 'desc' => $desc, 'hashtag' => $hashtag, 'subscribe' => !empty($mashsb_options['subscribe_behavior']) && $mashsb_options['subscribe_behavior'] === 'content' ? 'content' : 'link', 'subscribe_url' => isset($mashsb_options['subscribe_link']) ? $mashsb_options['subscribe_link'] : '', 'activestatus' => mashsbGetActiveStatus(), 'singular' => is_singular() ? 1 : 0, 'twitter_popup' => isset($mashsb_options['twitter_popup']) ? 0 : 1, 'refresh' => mashsb_is_cache_refresh() ? 1 : 0)); }
/** * Force cache refresh via GET REQUEST * * @global array $mashsb_options * @return boolean true for cache refresh */ function mashsb_force_cache_refresh() { global $mashsb_options; // Needed for testing (phpunit) if (MASHSB_DEBUG || isset($mashsb_options['disable_cache'])) { mashsb()->logger->info('mashsb_force_cache_refresh() -> Debug mode enabled'); return true; } $caching_method = !empty($mashsb_options['caching_method']) ? $mashsb_options['caching_method'] : 'refresh_loading'; // Old method and less performant - Cache is rebuild during pageload if ($caching_method == 'refresh_loading') { if (mashsb_is_cache_refresh()) { return true; } } // New method - Cache will be rebuild after complete pageloading and will be initiated via ajax. if (isset($_GET['mashsb-refresh']) && $caching_method == 'async_cache') { MASHSB()->logger->info('Force Cache Refresh'); return true; } }
/** * Check via ajax if cache should be updated * * @deprecated not used * @return string numerical */ function mashsb_ajax_refresh_cache() { if (mashsb_is_cache_refresh()) { wp_die('1'); } else { wp_die('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); } }