Example #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;
    }
}
Example #2
0
                load_textdomain('mashsb', $mofile_global);
            } elseif (file_exists($mofile_local)) {
                // Look in local /wp-content/plugins/mashshare/languages/ folder
                load_textdomain('mashsb', $mofile_local);
            } else {
                // Load the default language files
                load_plugin_textdomain('mashsb', false, $mashsb_lang_dir);
            }
        }
    }
}
// End if class_exists check
/**
 * The main function responsible for returning the one true Mashshare
 * Instance to functions everywhere.
 *
 * Use this function like you would a global variable, except without needing
 * to declare the global.
 *
 * Example: $MASHSB = MASHSB();
 *
 * @since 2.0.0
 * @return object The one true Mashshare Instance
 */
function MASHSB()
{
    return Mashshare::instance();
}
// Get MASHSB Running
MASHSB();
Example #3
0
 public function setRateLimitTransient()
 {
     set_transient('mashsb_rate_limit', 'true', 60 * 60);
     MASHSB()->logger->info('Error: Probably Facebook Rate Limit hit');
     $this->debug_notices[] = 'Error: Requests to Facebook probably hit Rate Limit.';
     add_action('wp_footer', array($this, 'outputDebug'), 100);
 }
Example #4
0
 function getCount($data, $url, $request_info, $service, $time)
 {
     $count = 0;
     if ($data) {
         switch ($service[0]) {
             case "facebook_likes":
                 $data = json_decode($data);
                 $count = is_array($data) ? $data[0]->like_count : $data->like_count;
                 break;
             case "facebook_shares":
                 $data = json_decode($data);
                 $count = is_array($data) ? $data[0]->share_count : $data->share_count;
                 break;
             case "facebook_total":
                 $data = json_decode($data);
                 $count = is_array($data) ? $data[0]->total_count : $data->total_count;
                 break;
             case "google":
                 preg_match('/window\\.__SSR = {c: ([\\d]+)/', $data, $matches);
                 if (isset($matches[0])) {
                     $count = str_replace('window.__SSR = {c: ', '', $matches[0]);
                 }
                 break;
             case "pinterest":
                 $data = substr($data, 13, -1);
             case "linkedin":
             case "twitter":
                 $data = json_decode($data);
                 $count = isset($data->count) ? $data->count : 0;
                 break;
             case "stumbleupon":
                 $data = json_decode($data);
                 isset($data->result->views) ? $count = $data->result->views : ($count = 0);
                 break;
             case "delicious":
                 $data = json_decode($data);
                 $count = !empty($data) ? $data[0]->total_posts : 0;
                 break;
             case "reddit":
                 $data = json_decode($data);
                 $ups = 0;
                 $downs = 0;
                 foreach ($data->data->children as $child) {
                     $ups += (int) $child->data->ups;
                     $downs += (int) $child->data->downs;
                 }
                 $count = $ups - $downs;
                 break;
             case "buffer":
                 $data = json_decode($data);
                 $count = !empty($data) ? $data->shares : 0;
                 //$count = $data->shares;
                 break;
             case "vk":
                 $data = preg_match('/^VK.Share.count\\(\\d+,\\s+(\\d+)\\);$/i', $data, $matches);
                 $count = $matches[1];
                 break;
             default:
                 // nothing here
         }
         $count = (int) $count;
         /*$this->data->shares->total += $count;
           $this->data->shares->$service[0] = $count;
            * */
         $this->data->total += $count;
         $this->data->{$service}[0] = $count;
         MASHSB()->logger->info('MashEngine - URL: ' . $url . ' ' . $service[0] . ': ' . $count);
     }
     return;
 }
Example #5
0
function mashsb_log_permissions()
{
    global $mashsb_options;
    if (!MASHSB()->logger->checkDir()) {
        return '<br><strong style="color:red;">' . __('Log file directory not writable! Set FTP permission to 755 or 777 for /wp-content/plugins/mashsharer/logs/', 'mashsb') . '</strong> <br> Read here more about <a href="http://codex.wordpress.org/Changing_File_Permissions" target="_blank">file permissions</a> ';
    }
}
/**
 * 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 get_sharedcount()
 {
     mashdebug()->info("Share URL: " . $this->url);
     global $mashsb_options;
     if (empty($mashsb_options['mashsharer_apikey'])) {
         return 0;
         //quit early if there's no API key.
     }
     $apikey = trim($mashsb_options['mashsharer_apikey']);
     $domain = trim($mashsb_options['mashsharer_sharecount_domain']);
     if (!isset($domain) || empty($domain)) {
         $domain = "free.sharedcount.com";
         $this->update_sharedcount_domain($apikey, $domain);
     }
     try {
         $counts = $this->_curl('http://' . $domain . "/?url=" . $this->url . "&apikey=" . $apikey);
         if (isset($counts["Error"]) && isset($counts['Domain']) && $counts["Type"] === "domain_apikey_mismatch") {
             $this->update_sharedcount_domain($apikey, $counts['Domain']);
             return 0;
         } else {
             if (isset($counts["Error"]) && isset($counts['Type']) && $counts['Type'] === 'invalid_api_key') {
                 $this->update_sharedcount_domain($apikey);
                 return 0;
             }
         }
         mashdebug()->info("Twitter count: " . isset($counts['Twitter']));
         MASHSB()->logger->info("URL: " . urldecode($this->url) . " API Key:" . $apikey . " sharedcount.com FB total_count: " . $counts['Facebook']['total_count'] . " FB share_count:" . $counts['Facebook']['share_count'] . " TW: " . $counts['Twitter'] . " G+:" . $counts['GooglePlusOne'] . " Linkedin:" . $counts['LinkedIn'] . " Stumble: " . $counts['StumbleUpon'] . " Pinterest: " . $counts['Pinterest']);
         return $counts;
     } catch (Exception $e) {
         mashdebug()->error("error: " . $counts);
         MASHSB()->logger->info('ERROR: Curl()' . $counts);
         return 0;
     }
     mashdebug()->error("error2: " . $counts);
     MASHSB()->logger->info('ERROR 2: Curl()' . $counts);
     return 0;
 }
Example #8
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);
    }
}
Example #9
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;
    }
}