Ejemplo n.º 1
0
function sw_twitter_button_html($array)
{
    // If we've already generated this button, just use our existing html
    if (isset($_GLOBALS['sw']['buttons'][$array['postID']]['twitter'])) {
        $array['resource']['twitter'] = $_GLOBALS['sw']['buttons'][$array['postID']]['twitter'];
        // If not, let's check if Facebook is activated and create the button HTML
    } elseif (isset($array['options']['twitter']) && $array['options']['twitter']) {
        $array['totes'] += $array['shares']['twitter'];
        ++$array['count'];
        $title = strip_tags(get_the_title($array['postID']));
        $title = str_replace('|', '', $title);
        $ct = get_post_meta($array['postID'], 'nc_customTweet', true);
        $ct = $ct != '' ? urlencode(html_entity_decode($ct, ENT_COMPAT, 'UTF-8')) : urlencode(html_entity_decode($title, ENT_COMPAT, 'UTF-8'));
        $twitterLink = sw_process_url($array['url'], 'twitter', $array['postID']);
        if (strpos($ct, 'http') !== false) {
            $urlParam = '&url=/';
        } else {
            $urlParam = '&url=' . $twitterLink;
        }
        if (sw_is_cache_fresh($array['postID']) == false) {
            $user_twitter_handle = get_the_author_meta('sw_twitter', sw_get_author($array['postID']));
            if ($user_twitter_handle) {
                delete_post_meta($array['postID'], 'sw_twitter_username');
                update_post_meta($array['postID'], 'sw_twitter_username', $user_twitter_handle);
            } else {
                delete_post_meta($array['postID'], 'sw_twitter_username');
            }
        } else {
            $user_twitter_handle = get_post_meta($array['postID'], 'sw_twitter_username', true);
        }
        if ($user_twitter_handle) {
            $viaText = '&via=' . $user_twitter_handle;
        } elseif ($array['options']['twitterID']) {
            $viaText = '&via=' . $array['options']['twitterID'];
        } else {
            $viaText = '';
        }
        $array['resource']['twitter'] = '<div class="nc_tweetContainer twitter" data-id="' . $array['count'] . '">';
        $array['resource']['twitter'] .= '<a href="https://twitter.com/share?original_referer=/&text=' . $ct . '' . $urlParam . '' . $viaText . '" data-link="https://twitter.com/share?original_referer=/&text=' . $ct . '' . $urlParam . '' . $viaText . '" class="nc_tweet">';
        if ($array['options']['totesEach'] && $array['totes'] >= $array['options']['minTotes'] && $array['shares']['twitter'] > 0) {
            $array['resource']['twitter'] .= '<span class="iconFiller">';
            $array['resource']['twitter'] .= '<span class="spaceManWilly">';
            $array['resource']['twitter'] .= '<i class="sw sw-twitter"></i>';
            $array['resource']['twitter'] .= '<span class="sw_share"> ' . $array['language']['twitter'] . '</span>';
            $array['resource']['twitter'] .= '</span></span>';
            $array['resource']['twitter'] .= '<span class="sw_count">' . kilomega($array['shares']['twitter']) . '</span>';
        } else {
            $array['resource']['twitter'] .= '<span class="sw_count sw_hide"><span class="iconFiller"><span class="spaceManWilly"><i class="sw sw-twitter"></i><span class="sw_share"> ' . $array['language']['twitter'] . '</span></span></span></span>';
        }
        $array['resource']['twitter'] .= '</a>';
        $array['resource']['twitter'] .= '</div>';
        // Store these buttons so that we don't have to generate them for each set
        $_GLOBALS['sw']['buttons'][$array['postID']]['twitter'] = $array['resource']['twitter'];
    }
    return $array;
}
function social_warfare_buttons($array = array())
{
    // Setup the default Array parameters
    if (!isset($array['where'])) {
        $array['where'] = 'default';
    }
    if (!isset($array['echo'])) {
        $array['echo'] = true;
    }
    if (!isset($array['content'])) {
        $array['content'] = false;
    }
    // Get the options...or create them if they don't exist
    if (isset($array['post_id'])) {
        $postID = $array['post_id'];
    } else {
        $postID = get_the_ID();
    }
    $options = sw_get_user_options();
    // Check to see if display location was specifically defined for this post
    $specWhere = get_post_meta($postID, 'nc_postLocation', true);
    if ($array['where'] == 'default') {
        if ($specWhere == 'default' || $specWhere == '') {
            if (is_singular('post')) {
                $array['where'] = $options['locationPost'];
            } elseif (is_singular('page')) {
                $array['where'] = $options['locationPage'];
            } elseif (is_singular()) {
                $postType = get_post_type($postID);
                if (isset($options['location' . $postType])) {
                    $array['where'] = $options['location' . $postType];
                } else {
                    $array['where'] = 'none';
                }
            } else {
                $postType = get_post_type($postID);
                if (isset($options['location' . $postType])) {
                    $array['where'] = $options['location' . $postType];
                } elseif ($postType == 'post' || $postType == 'page') {
                    $array['where'] = $options['locationSite'];
                } else {
                    $array['where'] = 'none';
                }
            }
        } else {
            $array['where'] = $specWhere;
        }
    }
    // Disable the buttons on Buddy Press pages
    if (function_exists('is_buddypress') && is_buddypress()) {
        return $array['content'];
        // Disable the buttons if the location is set to "None / Manual"
    } elseif ($array['where'] == 'none' && !isset($array['devs'])) {
        return $array['content'];
        // Disable the button if we're not in the loop, unless there is no content which means the function was called by a developer.
    } elseif ((!is_main_query() || !in_the_loop()) && !isset($array['devs'])) {
        return $array['content'];
        // Don't do anything if we're in the admin section
    } elseif (is_admin()) {
        return $array['content'];
        // If all the checks pass, let's make us some buttons!
    } else {
        // Set the options for the horizontal floating bar
        if ($options['float'] && is_singular()) {
            $floatOption = 'float' . ucfirst($options['floatOption']);
        } else {
            $floatOption = 'floatNone';
        }
        // Disable the plugin on feeds, search results, and non-published content
        if (!is_feed() && !is_search() && get_post_status($postID) == 'publish') {
            // Acquire the social stats from the networks
            if (isset($array['url'])) {
                $buttonsArray['url'] = $array['url'];
            } else {
                $buttonsArray['url'] = get_permalink($postID);
            }
            $language = array();
            $language = apply_filters('sw_languages', $language);
            // Setup the buttons array to pass into the 'sw_network_buttons' hook
            $buttonsArray['shares'] = get_social_warfare_shares($postID);
            $buttonsArray['language'] = apply_filters('sw_languages', $language);
            $buttonsArray['count'] = 0;
            $buttonsArray['totes'] = 0;
            $buttonsArray['options'] = $options;
            if ($buttonsArray['options']['totes'] && $buttonsArray['shares']['totes'] >= $buttonsArray['options']['minTotes']) {
                ++$buttonsArray['count'];
            }
            $buttonsArray['resource'] = array();
            $buttonsArray['postID'] = $postID;
            // Disable the subtitles plugin to avoid letting them inject their subtitle into our share titles
            if (is_plugin_active('subtitles/subtitles.php') && class_exists('Subtitles')) {
                remove_filter('the_title', array(Subtitles::getinstance(), 'the_subtitle'), 10, 2);
            }
            // This array will contain the HTML for all of the individual buttons
            $buttonsArray = apply_filters('sw_network_buttons', $buttonsArray);
            // Create the social panel
            $assets = '<div class="nc_socialPanel sw_' . $options['visualTheme'] . ' sw_d_' . $options['dColorSet'] . ' sw_i_' . $options['iColorSet'] . ' sw_o_' . $options['oColorSet'] . '" data-position="' . $options['locationPost'] . '" data-float="' . $floatOption . '" data-count="' . $buttonsArray['count'] . '" data-floatColor="' . $options['floatBgColor'] . '" data-scale="' . $options['buttonSize'] . '" data-align="' . $options['buttonFloat'] . '">';
            // Setup the total shares count if it's on the left
            if ($options['totes'] && $options['swTotesFormat'] == 'totesAltLeft' && $buttonsArray['totes'] >= $options['minTotes']) {
                $assets .= '<div class="nc_tweetContainer totes totesalt" data-id="6" >';
                $assets .= '<span class="sw_count">' . kilomega($buttonsArray['totes']) . ' <span class="sw_label">' . $language['total'] . '</span></span>';
                $assets .= '</div>';
            }
            // Sort the buttons according to the user's preferences
            if ($options['orderOfIconsSelect'] == 'manual') {
                foreach ($options['newOrderOfIcons'] as $thisIcon => $status) {
                    if (isset($buttonsArray['resource'][$thisIcon])) {
                        $assets .= $buttonsArray['resource'][$thisIcon];
                    }
                }
            } elseif ($options['orderOfIconsSelect'] == 'dynamicCount') {
                arsort($buttonsArray['shares']);
                foreach ($buttonsArray['shares'] as $thisIcon => $status) {
                    if (isset($buttonsArray['resource'][$thisIcon])) {
                        $assets .= $buttonsArray['resource'][$thisIcon];
                    }
                }
            }
            // Create the Total Shares Box if it's on the right
            if ($options['totes'] && $options['swTotesFormat'] != 'totesAltLeft' && $buttonsArray['totes'] >= $options['minTotes']) {
                if ($options['swTotesFormat'] == 'totes') {
                    $assets .= '<div class="nc_tweetContainer totes" data-id="6" >';
                    $assets .= '<span class="sw_count">' . kilomega($buttonsArray['totes']) . ' <span class="sw_label">' . $language['total'] . '</span></span>';
                    $assets .= '</div>';
                } else {
                    $assets .= '<div class="nc_tweetContainer totes totesalt" data-id="6" >';
                    $assets .= '<span class="sw_count"><span class="sw_label">' . $language['total'] . '</span> ' . kilomega($buttonsArray['totes']) . '</span>';
                    $assets .= '</div>';
                }
            }
            // Close the Social Panel
            $assets .= '</div>';
            // Reset the cache timestamp if needed
            if (sw_is_cache_fresh($postID) == false) {
                delete_post_meta($postID, 'sw_cache_timestamp');
                update_post_meta($postID, 'sw_cache_timestamp', floor(date('U') / 60 / 60));
            }
            if ($array['echo'] == false && $array['where'] != 'none') {
                return $assets;
            } elseif ($array['content'] === false) {
                echo $assets;
            } elseif ($array['where'] == 'below') {
                $content = $array['content'] . '' . $assets;
                return $content;
            } elseif ($array['where'] == 'above') {
                $content = $assets . '' . $array['content'];
                return $content;
            } elseif ($array['where'] == 'both') {
                $content = $assets . '' . $array['content'] . '' . $assets;
                return $content;
            } elseif ($array['where'] == 'none') {
                return $array['content'];
            }
        } else {
            return $array['content'];
        }
    }
}
Ejemplo n.º 3
0
function sw_pinterest_button_html($array)
{
    // If we've already generated this button, just use our existing html
    if (isset($_GLOBALS['sw']['buttons'][$array['postID']]['pinterest'])) {
        $array['resource']['pinterest'] = $_GLOBALS['sw']['buttons'][$array['postID']]['pinterest'];
        // If not, let's check if Facebook is activated and create the button HTML
    } elseif ($array['options']['pinterest']) {
        $array['totes'] += $array['shares']['pinterest'];
        ++$array['count'];
        $pi = get_post_meta($array['postID'], 'nc_pinterestImage', true);
        if (sw_is_cache_fresh($array['postID']) == false) {
            // Check if an image ID has been provided
            $array['imageID'] = get_post_meta($array['postID'], 'nc_pinterestImage', true);
            if ($array['imageID']) {
                $array['imageURL'] = wp_get_attachment_url($array['imageID']);
                delete_post_meta($array['postID'], 'sw_pinterest_image_url');
                update_post_meta($array['postID'], 'sw_pinterest_image_url', $array['imageURL']);
            } else {
                $array['imageURL'] = wp_get_attachment_url(get_post_thumbnail_id($array['postID']));
                delete_post_meta($array['postID'], 'sw_pinterest_image_url');
            }
        } else {
            // Check if we have a cached Open Graph Image URL
            $array['imageURL'] = get_post_meta($array['postID'], 'sw_pinterest_image_url', true);
            // If not, let's check to see if we have an ID to generate one
            if (!$array['imageURL']) {
                // Check for an Open Graph Image ID
                $array['imageID'] = get_post_meta($array['postID'], 'nc_pinterestImage', true);
                if ($array['imageID']) {
                    // If we find one, let's convert it to a link and cache it for next time
                    $array['imageURL'] = wp_get_attachment_url($array['imageID']);
                    delete_post_meta($array['postID'], 'sw_pinterest_image_url');
                    update_post_meta($array['postID'], 'sw_pinterest_image_url', $array['imageURL']);
                } else {
                    // If we don't find one, let's see if we can use a post thumbnail
                    $array['imageURL'] = wp_get_attachment_url(get_post_thumbnail_id($array['postID']));
                }
            }
        }
        $pd = get_post_meta($array['postID'], 'nc_pinterestDescription', true);
        if ($array['imageURL']) {
            $pi = '&media=' . urlencode(html_entity_decode($array['imageURL'], ENT_COMPAT, 'UTF-8'));
        } else {
            $pi = '';
        }
        $pinterestLink = $array['url'];
        $title = strip_tags(get_the_title($array['postID']));
        $title = str_replace('|', '', $title);
        if ($pi != '') {
            $a = '<a data-link="https://pinterest.com/pin/create/button/?url=' . $pinterestLink . '' . $pi . '&description=' . ($pd != '' ? urlencode(html_entity_decode($pd, ENT_COMPAT, 'UTF-8')) : urlencode(html_entity_decode($title, ENT_COMPAT, 'UTF-8'))) . '" class="nc_tweet" data-count="0">';
        } else {
            $a = '<a onClick="var e=document.createElement(\'script\');e.setAttribute(\'type\',\'text/javascript\');e.setAttribute(\'charset\',\'UTF-8\');e.setAttribute(\'src\',\'//assets.pinterest.com/js/pinmarklet.js?r=\'+Math.random()*99999999);document.body.appendChild(e);" class="nc_tweet noPop">';
        }
        $array['resource']['pinterest'] = '<div class="nc_tweetContainer nc_pinterest" data-id="' . $array['count'] . '">';
        $array['resource']['pinterest'] .= $a;
        if ($array['options']['totesEach'] && $array['totes'] >= $array['options']['minTotes'] && $array['shares']['pinterest'] > 0) {
            $array['resource']['pinterest'] .= '<span class="iconFiller">';
            $array['resource']['pinterest'] .= '<span class="spaceManWilly" style="width:55px;">';
            $array['resource']['pinterest'] .= '<i class="sw sw-pinterest"></i>';
            $array['resource']['pinterest'] .= '<span class="sw_share"> ' . $array['language']['pinterest'] . '</span>';
            $array['resource']['pinterest'] .= '</span></span>';
            $array['resource']['pinterest'] .= '<span class="sw_count">' . kilomega($array['shares']['pinterest']) . '</span>';
        } else {
            $array['resource']['pinterest'] .= '<span class="sw_count sw_hide"><span class="iconFiller"><span class="spaceManWilly" style="width:55px;"><i class="sw sw-pinterest"></i><span class="sw_share"> ' . $array['language']['pinterest'] . '</span></span></span></span>';
        }
        $array['resource']['pinterest'] .= '</a>';
        $array['resource']['pinterest'] .= '</div>';
        // Store these buttons so that we don't have to generate them for each set
        $_GLOBALS['sw']['buttons'][$array['postID']]['pinterest'] = $array['resource']['pinterest'];
    }
    return $array;
}
function get_social_warfare_shares($postID)
{
    // Set the initial options
    $options = sw_get_user_options();
    $url = get_permalink($postID);
    // $url				= 'https://youtu.be/jjK1aUU2Dx4';
    /*****************************************************************
    *                                                                *
    *        Check the Cache		                    			 *
    *                                                                *
    ******************************************************************/
    $freshCache = sw_is_cache_fresh($postID);
    // $freshCache = false;
    /*****************************************************************
    *                                                                *
    *       Setup the Networks Array that we'll loop through		 *
    *                                                                *
    ******************************************************************/
    // Initiate the ShareCount class
    $shares['totes'] = 0;
    // Queue up the networks that are available
    $availableNetworks = $options['newOrderOfIcons'];
    $networks = array();
    foreach ($availableNetworks as $key => $value) {
        if ($options[$key]) {
            $networks[] = $key;
        }
    }
    /*****************************************************************
    *                                                                *
    *       Loop through the Networks                    			 *
    *                                                                *
    ******************************************************************/
    // Loop through the networks and fetch their share counts
    foreach ($networks as $network) {
        // Check if this network is even activated
        if ($options[$network]) {
            // Check if we can used the cached share numbers
            if ($freshCache == true) {
                $shares[$network] = get_post_meta($postID, '_' . $network . '_shares', true);
                // If cache is expired, fetch new and update the cache
            } else {
                $old_shares[$network] = get_post_meta($postID, '_' . $network . '_shares', true);
                $share_links[$network] = call_user_func('sw_' . $network . '_request_link', $url);
            }
        }
    }
    // Recover Shares From Previously Used URL Patterns
    if ($options['recover_shares'] == true && $freshCache == false) {
        $alternateURL = sw_get_alternate_permalink($options['recovery_format'], $options['recovery_protocol'], $postID);
        foreach ($networks as $network) {
            // Check if this network is even activated
            if ($options[$network]) {
                $old_share_links[$network] = call_user_func('sw_' . $network . '_request_link', $alternateURL);
            }
        }
    }
    if ($freshCache == true) {
        $shares['totes'] = get_post_meta($postID, '_totes', true);
    } else {
        // Fetch all the share counts asyncrounously
        $raw_shares_array = sw_fetch_shares_via_curl_multi($share_links);
        if ($options['recover_shares'] == true) {
            $old_raw_shares_array = sw_fetch_shares_via_curl_multi($old_share_links);
        }
        foreach ($networks as $network) {
            // Check if this network is even activated
            if ($options[$network]) {
                if (!isset($raw_shares_array[$network])) {
                    $raw_shares_array[$network] = 0;
                }
                if (!isset($old_raw_shares_array[$network])) {
                    $old_raw_shares_array[$network] = 0;
                }
                $shares[$network] = call_user_func('sw_format_' . $network . '_response', $raw_shares_array[$network]);
                if ($options['recover_shares'] == true) {
                    $recovered_shares[$network] = call_user_func('sw_format_' . $network . '_response', $old_raw_shares_array[$network]);
                    if ($shares[$network] != $recovered_shares[$network]) {
                        $shares[$network] = $shares[$network] + $recovered_shares[$network];
                    }
                }
                if ($shares[$network] <= $old_shares[$network]) {
                    $shares[$network] = $old_shares[$network];
                } else {
                    delete_post_meta($postID, '_' . $network . '_shares');
                    update_post_meta($postID, '_' . $network . '_shares', $shares[$network]);
                }
                $shares['totes'] += $shares[$network];
            }
        }
    }
    /*****************************************************************
    *                                                                *
    *       Update the Cache and Return the Share Counts   			 *
    *                                                                *
    ******************************************************************/
    if ($freshCache != true) {
        // Clean out the previously used custom meta fields
        delete_post_meta($postID, '_totes');
        // Add the new data to the custom meta fields
        update_post_meta($postID, '_totes', $shares['totes']);
    }
    // Return the share counts
    return $shares;
}
Ejemplo n.º 5
0
function sw_bitly_shortener($array)
{
    $url = $array['url'];
    $network = $array['network'];
    $postID = $array['postID'];
    // Fetch the User's Options
    $options = sw_get_user_options();
    // If Link shortening is activated....
    if ($options['linkShortening'] == true) {
        // If Bitly is activated and we have all the appropriate credentials....
        if ($options['shorteningMethod'] == 'bitly' && isset($options['bitlyLogin']) && isset($options['bitlyAPI'])) {
            // Collect our bitly login information
            $login = $options['bitlyLogin'];
            $appkey = $options['bitlyAPI'];
            // If Google Analytics is Activated....
            if ($options['googleAnalytics'] == true) {
                // If the Cache is still fresh or a previous API request failed....
                if (sw_is_cache_fresh($postID) == true || isset($_GLOBALS['bitly_status']) && $_GLOBALS['bitly_status'] == 'failure') {
                    // If the link has already been shortened....
                    $existingURL = get_post_meta($postID, 'bitly_link_' . $network, true);
                    if ($existingURL && sw_is_cache_fresh($postID) == true) {
                        return $existingURL;
                        // If the link has NOT already been shortened
                    } else {
                        // ....Return the normal URL
                        return $url;
                    }
                    // If the Cache is NOT fresh....
                } else {
                    // If the API provides a shortened URL...
                    $shortURL = sw_make_bitly_url(urldecode($url), $network, $login, $appkey);
                    if ($shortURL) {
                        // Store the link in the cache and return it to the buttons
                        delete_post_meta($postID, 'bitly_link_' . $network);
                        update_post_meta($postID, 'bitly_link_' . $network, $shortURL);
                        return $shortURL;
                        // If the API does not provide a shortened URL....
                    } else {
                        // Set a variable we'll check to avoid multiple calls to bitly upon the first failure
                        $_GLOBALS['sw']['bitly_status'] = 'failure';
                        // Return the normal URL
                        return $url;
                        // End the check for a shortneing link from the API
                    }
                    // End the check for the cache being fresh
                }
                // If Google Analytics is NOT activated....
            } else {
                // If the cache is fresh or if the API has failed already....
                if (sw_is_cache_fresh($postID) == true || isset($_GLOBALS['bitly_status']) && $_GLOBALS['bitly_status'] == 'failure') {
                    // If we have a shortened URL in the cache....
                    $existingURL = get_post_meta($postID, 'bitly_link', true);
                    if ($existingURL) {
                        // Save the link in a constant for use in other parts of the loops
                        $_GLOBALS['sw']['links'][$postID] = $existingURL;
                        // Return the shortened URL
                        return $existingURL;
                        // If we don't have a shortlink in the cache....
                    } else {
                        // Return the normal URL
                        return $url;
                    }
                    // If the cache is expired and needs to be rebuilt....
                } else {
                    // If we've already generated this link....
                    if (isset($_GLOBALS['sw']['links'][$postID])) {
                        return $_GLOBALS['sw']['links'][$postID];
                        // If we've don't already have a generated link....
                    } else {
                        // Use the bitly function to construct a shortened link
                        $shortURL = sw_make_bitly_url(urldecode($url), $network, $login, $appkey);
                        // If we got a shortened URL from their API....
                        if ($shortURL) {
                            // Save the link in a global so we can skip this part next time
                            $_GLOBALS['sw']['links'][$postID] = $shortURL;
                            // Delete the meta fields and then update to keep the database clean and up to date.
                            delete_post_meta($postID, 'bitly_link_' . $network);
                            delete_post_meta($postID, 'bitly_link');
                            update_post_meta($postID, 'bitly_link', $shortURL);
                            // Return the normal URL
                            return $shortURL;
                            // If didn't get a shortened URL from their API....
                        } else {
                            // Set a variable we'll check to avoid multiple calls to bitly upon the first failure
                            $_GLOBALS['sw']['bitly_status'] = 'failure';
                            // Return the normal URL
                            return $url;
                            // End check for shorte URL from the API
                        }
                        // End check for link in the Global Variable
                    }
                    // End check for the cache freshness
                }
                // End check for Analytics
            }
            // If Bitly is not activated or we don't have the credentials provided....
        } else {
            // Return the normal URL
            return $url;
            // End the check for bitly activation and credentials
        }
        // If link shortening is not activated....
    } else {
        // Return the normal URL
        return $url;
        // End the check for link shortening being activated
    }
}
function sw_output_cache_trigger($info)
{
    if (is_singular() && sw_is_cache_fresh(get_the_ID(), true) == false && $info['sw_user_options']['cacheMethod'] != 'legacy') {
        $url = get_the_permalink();
        if (strpos($url, '?') === false) {
            $url = $url . '?sw_cache=rebuild';
        } else {
            $url = $url . '&sw_cache=rebuild';
        }
        $info['header_output'] .= PHP_EOL . '<script type="text/javascript">document.addEventListener("DOMContentLoaded", function(event) { jQuery.get("' . $url . '"); });</script>';
    }
    return $info;
}