function swp_cache_rebuild() { // Gain access to the database global $wpdb; // Fetch the Post ID $post_id = $_POST['post_id']; // Ensure that the cache for this post is actually expired if (swp_is_cache_fresh($post_id, true, true) == false) { // Force the cache trigger on $_GET['swp_cache'] = 'rebuild'; // Fetch new shares $shares = get_social_warfare_shares($post_id); // Update the cache timestamp delete_post_meta($post_id, 'swp_cache_timestamp'); update_post_meta($post_id, 'swp_cache_timestamp', floor(date('U') / 60 / 60)); // Return the share count wp_send_json($shares); } // Kill off all the WordPress functions wp_die(); }
function swp_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']['newOrderOfIcons']['twitter']) && !isset($array['buttons']) || isset($array['buttons']) && isset($array['buttons']['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 = swp_process_url($array['url'], 'twitter', $array['postID']); if (strpos($ct, 'http') !== false) { $urlParam = '&url=/'; } else { $urlParam = '&url=' . $twitterLink; } if (swp_is_cache_fresh($array['postID']) == false) { $user_twitter_handle = get_the_author_meta('swp_twitter', swp_get_author($array['postID'])); if ($user_twitter_handle) { delete_post_meta($array['postID'], 'swp_twitter_username'); update_post_meta($array['postID'], 'swp_twitter_username', $user_twitter_handle); } else { delete_post_meta($array['postID'], 'swp_twitter_username'); } } else { $user_twitter_handle = get_post_meta($array['postID'], 'swp_twitter_username', true); } if ($user_twitter_handle) { $viaText = '&via=' . str_replace('@', '', $user_twitter_handle); } elseif ($array['options']['twitterID']) { $viaText = '&via=' . str_replace('@', '', $array['options']['twitterID']); } else { $viaText = ''; } $array['resource']['twitter'] = '<div class="nc_tweetContainer twitter" data-id="' . $array['count'] . '" data-network="twitter">'; $array['resource']['twitter'] .= '<a rel="nofollow" target="_blank" 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['shares']['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="swp_share"> ' . __('Tweet', 'social-warfare') . '</span>'; $array['resource']['twitter'] .= '</span></span>'; $array['resource']['twitter'] .= '<span class="swp_count">' . swp_kilomega($array['shares']['twitter']) . '</span>'; } else { $array['resource']['twitter'] .= '<span class="swp_count swp_hide"><span class="iconFiller"><span class="spaceManWilly"><i class="sw sw-twitter"></i><span class="swp_share"> ' . __('Tweet', 'social-warfare') . '</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 swp_bitly_shortener($array) { $url = $array['url']; $network = $array['network']; $postID = $array['postID']; // Fetch the User's Options $options = swp_get_user_options(); // If Link shortening is activated.... if ($options['linkShortening'] == true) { // If Bitly is activated and we have all the appropriate credentials.... if (isset($options['bitly_access_token'])) { // Collect our bitly login information $access_token = $options['bitly_access_token']; // If Google Analytics is Activated.... if ($options['googleAnalytics'] == true) { // If the Cache is still fresh or a previous API request failed.... if (swp_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 && swp_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 = swp_make_bitly_url(urldecode($url), $network, $access_token); 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 (swp_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 = swp_make_bitly_url(urldecode($url), $network, $access_token); // 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 short 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 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 = swp_get_user_options(); // Check to see if display location was specifically defined for this post $specWhere = get_post_meta($postID, 'nc_postLocation', true); if (!$specWhere) { $specWhere = 'default'; } if ($array['where'] == 'default') { // If we are on a single page or post if (is_singular() && !is_home() && !is_archive()) { // Make sure this is the main loop //if( get_permalink( $postID ) == swp_get_current_url() ) : // Check if a specific display value has not been set for this specific post if ($specWhere == 'default' || $specWhere == '') { $postType = get_post_type($postID); if (isset($options['location_' . $postType])) { $array['where'] = $options['location_' . $postType]; } else { $array['where'] = 'none'; } } else { $array['where'] = $specWhere; } // If it's not the main loop //else: // $array['where'] = 'none'; //endif; // If we are on an archive or home page } else { $array['where'] = $options['locationSite']; } } // 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 $postType = get_post_type($postID); $spec_float_where = get_post_meta($postID, 'nc_floatLocation', true); if (isset($array['float']) && $array['float'] == 'ignore') { $floatOption = 'float_ignore'; } elseif ($spec_float_where == 'off' && $options['buttonFloat'] != 'float_ignore') { $floatOption = 'floatNone'; } elseif ($options['float'] && is_singular() && $options['float_location_' . $postType] == 'on') { $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); } // Fetch the share counts $buttonsArray['shares'] = get_social_warfare_shares($postID); // Pass the swp_options into the array so we can pass it into the filter $buttonsArray['options'] = $options; // Customize which buttosn we're going to display if (isset($array['buttons'])) { // Fetch the global names and keys $swp_options = array(); $swp_available_options = apply_filters('swp_options', $swp_options); $available_buttons = $swp_available_options['options']['swp_display']['buttons']['content']; // Split the comma separated list into an array $button_set_array = explode(',', $array['buttons']); // Match the names in the list to their appropriate system-wide keys foreach ($button_set_array as $button) { // Trim the network name in case of white space $button = trim($button); // Convert the names to their systme-wide keys if (recursive_array_search($button, $available_buttons)) { $key = recursive_array_search($button, $available_buttons); // Store the result in the array that gets passed to the HTML generator $buttonsArray['buttons'][$key] = $button; // Declare a default share count of zero. This will be overriden later if (!isset($buttonsArray['shares'][$key])) { $buttonsArray['shares'][$key] = 0; } } } // Manually turn the total shares on or off if (array_search('Total', $button_set_array)) { $buttonsArray['buttons']['totes'] = 'Total'; } } // Setup the buttons array to pass into the 'swp_network_buttons' hook $buttonsArray['count'] = 0; $buttonsArray['totes'] = 0; if ($buttonsArray['options']['totes'] && $buttonsArray['shares']['totes'] >= $buttonsArray['options']['minTotes'] && !isset($array['buttons']) || isset($buttonsArray['buttons']) && isset($buttonsArray['buttons']['totes']) && $buttonsArray['totes'] >= $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('swp_network_buttons', $buttonsArray); // Create the social panel $assets = '<div class="nc_socialPanel swp_' . $options['visualTheme'] . ' swp_d_' . $options['dColorSet'] . ' swp_i_' . $options['iColorSet'] . ' swp_o_' . $options['oColorSet'] . '" data-position="' . $options['location_post'] . '" 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'] && !isset($array['buttons']) || $options['swTotesFormat'] == 'totesAltLeft' && isset($array['buttons']) && isset($array['buttons']['totes']) && $buttonsArray['totes'] >= $options['minTotes']) { ++$buttonsArray['count']; $assets .= '<div class="nc_tweetContainer totes totesalt" data-id="' . $buttonsArray['count'] . '" >'; $assets .= '<span class="swp_count">' . swp_kilomega($buttonsArray['totes']) . ' <span class="swp_label">' . __('Shares', 'social-warfare') . '</span></span>'; $assets .= '</div>'; } // Sort the buttons according to the user's preferences if (isset($buttonsArray) && isset($buttonsArray['buttons'])) { foreach ($buttonsArray['buttons'] as $key => $value) { if (isset($buttonsArray['resource'][$key])) { $assets .= $buttonsArray['resource'][$key]; } } } elseif ($options['orderOfIconsSelect'] == 'manual') { foreach ($options['newOrderOfIcons'] as $key => $value) { if (isset($buttonsArray['resource'][$key])) { $assets .= $buttonsArray['resource'][$key]; } } } elseif ($options['orderOfIconsSelect'] == 'dynamic') { 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'] && !isset($buttonsArray['buttons']) || $options['swTotesFormat'] != 'totesAltLeft' && isset($buttonsArray['buttons']) && isset($buttonsArray['buttons']['totes']) && $buttonsArray['totes'] >= $options['minTotes']) { ++$buttonsArray['count']; if ($options['swTotesFormat'] == 'totes') { $assets .= '<div class="nc_tweetContainer totes" data-id="' . $buttonsArray['count'] . '" >'; $assets .= '<span class="swp_count">' . swp_kilomega($buttonsArray['totes']) . ' <span class="swp_label">' . __('Shares', 'social-warfare') . '</span></span>'; $assets .= '</div>'; } else { $assets .= '<div class="nc_tweetContainer totes totesalt" data-id="' . $buttonsArray['count'] . '" >'; $assets .= '<span class="swp_count"><span class="swp_label">' . __('Shares', 'social-warfare') . '</span> ' . swp_kilomega($buttonsArray['totes']) . '</span>'; $assets .= '</div>'; } } // Close the Social Panel $assets .= '</div>'; // Reset the cache timestamp if needed if (swp_is_cache_fresh($postID) == false) { delete_post_meta($postID, 'swp_cache_timestamp'); update_post_meta($postID, 'swp_cache_timestamp', floor(date('U') / 60 / 60)); } if (isset($array['genesis'])) { if ($array['where'] == 'below' && $array['genesis'] == 'below') { return $assets; } elseif ($array['where'] == 'above' && $array['genesis'] == 'above') { return $assets; } elseif ($array['where'] == 'both') { return $assets; } elseif ($array['where'] == 'none') { return false; } } else { 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']; } } }
function swp_output_cache_trigger($info) { // Check if we're on a single post page, the cache is expired, and they're using the updated cache rebuild method if (is_singular() && swp_is_cache_fresh(get_the_ID(), true) == false && $info['swp_user_options']['cacheMethod'] != 'legacy') { // Make sure we're not on a WooCommerce Account Page if (is_plugin_active('woocommerce/woocommerce.php') && is_account_page()) { return $info; // Trigger the cache rebuild } else { $url = get_permalink(); $admin_ajax = admin_url('admin-ajax.php'); $info['footer_output'] .= PHP_EOL . 'var swp_buttons_exist = !!document.getElementsByClassName("nc_socialPanel");if(swp_buttons_exist) {jQuery(document).on( "ready" , function() { var swp_admin_ajax = "' . $admin_ajax . '";var swp_cache_data = {"action":"swp_cache_trigger","post_id":' . $info['postID'] . '};jQuery.post(swp_admin_ajax, swp_cache_data, function(response) {console.log(response);});});}'; } } // Return the array so the world doesn't explode return $info; }
function swp_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']['newOrderOfIcons']['pinterest'] && !isset($array['buttons']) || isset($array['buttons']) && isset($array['buttons']['pinterest'])) { $array['totes'] += $array['shares']['pinterest']; ++$array['count']; $pi = get_post_meta($array['postID'], 'nc_pinterestImage', true); // Pinterest Username $pinterest_username = $array['options']['pinterestID']; if (isset($pinterest_username) && $pinterest_username != '') { $pu = ' via @' . str_replace('@', '', $pinterest_username); } else { $pu = ''; } if (swp_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'], 'swp_pinterest_image_url'); update_post_meta($array['postID'], 'swp_pinterest_image_url', $array['imageURL']); // else: // $array['imageURL'] = wp_get_attachment_url( get_post_thumbnail_id( $array['postID'] ) ); // delete_post_meta($array['postID'],'swp_pinterest_image_url'); } } else { // Check if we have a cached Open Graph Image URL $array['imageURL'] = get_post_meta($array['postID'], 'swp_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'], 'swp_pinterest_image_url'); update_post_meta($array['postID'], 'swp_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 != '' && is_swp_registered()) { $a = '<a data-link="https://pinterest.com/pin/create/button/?url=' . $pinterestLink . '' . $pi . '&description=' . ($pd != '' ? urlencode(html_entity_decode($pd . $pu, ENT_COMPAT, 'UTF-8')) : urlencode(html_entity_decode($title . $pu, 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'] . '" data-network="pinterest">'; $array['resource']['pinterest'] .= $a; if ($array['options']['totesEach'] && $array['shares']['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="swp_share"> ' . __('Pin', 'social-warfare') . '</span>'; $array['resource']['pinterest'] .= '</span></span>'; $array['resource']['pinterest'] .= '<span class="swp_count">' . swp_kilomega($array['shares']['pinterest']) . '</span>'; } else { $array['resource']['pinterest'] .= '<span class="swp_count swp_hide"><span class="iconFiller"><span class="spaceManWilly" style="width:55px;"><i class="sw sw-pinterest"></i><span class="swp_share"> ' . __('Pin', 'social-warfare') . '</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 = swp_get_user_options(); $url = get_permalink($postID); // $url = 'https://youtu.be/jjK1aUU2Dx4'; /***************************************************************** * * * Check the Cache * * * ******************************************************************/ $freshCache = swp_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['newOrderOfIcons'][$key]) { $networks[] = $key; } } /***************************************************************** * * * Loop through the Networks * * * ******************************************************************/ // Loop through the networks and fetch their share counts foreach ($networks as $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('swp_' . $network . '_request_link', $url); } } // Recover Shares From Previously Used URL Patterns if ($options['recover_shares'] == true && $freshCache == false) { $alternateURL = swp_get_alt_permalink($postID); $alternateURL = apply_filters('swp_recovery_filter', $alternateURL); // Debug the Alternate URL being checked if (isset($_GET['swp_recovery_debug']) && $_GET['swp_recovery_debug'] == true) { echo $alternateURL; } foreach ($networks as $network) { $old_share_links[$network] = call_user_func('swp_' . $network . '_request_link', $alternateURL); } } if ($freshCache == true) { if (get_post_meta($postID, '_totes', true)) { $shares['totes'] = get_post_meta($postID, '_totes', true); } else { $shares['totes'] = 0; } } else { // Fetch all the share counts asyncrounously $raw_shares_array = swp_fetch_shares_via_curl_multi($share_links); if ($options['recover_shares'] == true) { $old_raw_shares_array = swp_fetch_shares_via_curl_multi($old_share_links); } foreach ($networks as $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('swp_format_' . $network . '_response', $raw_shares_array[$network]); if ($options['recover_shares'] == true) { $recovered_shares[$network] = call_user_func('swp_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; }
/** * Trigger cache rebuild. * * @since 1.4.7 * @access public * @param array $info An array of footer script information. * @return array $info A modified array of footer script information. */ function swp_output_cache_trigger($info) { // Bail early if we're not on a single page or we have fresh cache. if (!is_singular() || swp_is_cache_fresh(get_the_ID(), true)) { return $info; } // Bail if we're not using the newer cache method. if ('legacy' === $info['swp_user_options']['cacheMethod']) { return $info; } // Bail if we're on a WooCommerce account page. if (function_exists('is_account_page') && is_account_page()) { return $info; } // Trigger the cache rebuild. if ('rebuild' === get_query_var('swp_cache') || false === swp_is_cache_fresh(get_the_ID(), true)) { ob_start(); ?> swp_admin_ajax = '<?php echo admin_url('admin-ajax.php'); ?> '; var swp_buttons_exist = !!document.getElementsByClassName( 'nc_socialPanel' ); if ( swp_buttons_exist ) { jQuery( document ).ready( function() { var swp_cache_data = { 'action': 'swp_cache_trigger', 'post_id': <?php echo $info['postID']; ?> }; jQuery.post( swp_admin_ajax, swp_cache_data, function( response ) { console.log(response); }); }); } swp_post_id='<?php echo $info['postID']; ?> '; swp_post_url='<?php echo get_permalink(); ?> '; socialWarfarePlugin.fetchFacebookShares(); <?php $info['footer_output'] = ob_get_clean(); } return $info; }
function swp_add_header_meta() { global $swp_user_options; $info['postID'] = get_the_ID(); // Cache some resource for fewer queries on subsequent page loads if (swp_is_cache_fresh($info['postID'], true) == false) { // Check if an image ID has been provided $info['imageID'] = get_post_meta($info['postID'], 'nc_ogImage', true); if ($info['imageID']) { // Cache the image URL $info['imageURL'] = wp_get_attachment_url($info['imageID']); delete_post_meta($info['postID'], 'swp_open_graph_image_url'); update_post_meta($info['postID'], 'swp_open_graph_image_url', $info['imageURL']); // Cache the height and width $info['image_data'] = wp_get_attachment_image_src($info['imageID'], 'full'); delete_post_meta($info['postID'], 'swp_open_graph_image_data'); update_post_meta($info['postID'], 'swp_open_graph_image_data', json_encode($info['image_data'])); } else { $info['imageURL'] = wp_get_attachment_url(get_post_thumbnail_id($info['postID'])); delete_post_meta($info['postID'], 'swp_open_thumbnail_url'); update_post_meta($info['postID'], 'swp_open_thumbnail_url', $info['imageURL']); delete_post_meta($info['postID'], 'swp_open_graph_image_url'); // Cache the height and width $info['image_data'] = wp_get_attachment_image_src(get_post_thumbnail_id($info['postID']), 'full'); delete_post_meta($info['postID'], 'swp_open_graph_image_data'); update_post_meta($info['postID'], 'swp_open_graph_image_data', json_encode($info['image_data'])); } // Cache the Twitter Handle $user_twitter_handle = get_the_author_meta('swp_twitter', swp_get_author($info['postID'])); if ($user_twitter_handle) { delete_post_meta($info['postID'], 'swp_twitter_username'); update_post_meta($info['postID'], 'swp_twitter_username', $user_twitter_handle); } else { delete_post_meta($info['postID'], 'swp_twitter_username'); } } else { // Check if we have a cached Open Graph Image URL $info['imageURL'] = get_post_meta($info['postID'], 'swp_open_graph_image_url', true); // If not, let's check to see if we have an ID to generate one if (!$info['imageURL']) { // Check for an Open Graph Image ID $info['imageID'] = get_post_meta($info['postID'], 'nc_ogImage', true); if ($info['imageID']) { // If we find one, let's convert it to a link and cache it for next time $info['imageURL'] = wp_get_attachment_url($info['imageID']); delete_post_meta($info['postID'], 'swp_open_graph_image_url'); update_post_meta($info['postID'], 'swp_open_graph_image_url', $info['imageURL']); } else { // If we don't find one, let's save the URL of the thumbnail in case we need it $thumbnail_image = get_post_meta($info['postID'], 'swp_open_thumbnail_url', true); } } $user_twitter_handle = get_post_meta($info['postID'], 'swp_twitter_username', true); } // Create the image Open Graph Meta Tag $info['postID'] = get_the_ID(); $info['title'] = htmlspecialchars(get_post_meta($info['postID'], 'nc_ogTitle', true)); $info['description'] = htmlspecialchars(get_post_meta($info['postID'], 'nc_ogDescription', true)); $info['swp_fb_author'] = htmlspecialchars(get_post_meta($info['postID'], 'swp_fb_author', true)); $info['swp_user_options'] = $swp_user_options; $info['user_twitter_handle'] = $user_twitter_handle; $info['header_output'] = ''; $info = apply_filters('swp_meta_tags', $info); if ($info['header_output']) { echo PHP_EOL . '<!-- Open Graph Meta Tags & Twitter Card generated by Social Warfare v' . SWP_VERSION . ' http://warfareplugins.com -->'; echo $info['header_output']; echo PHP_EOL . '<!-- Open Graph Meta Tags & Twitter Card generated by Social Warfare v' . SWP_VERSION . ' http://warfareplugins.com -->' . PHP_EOL . PHP_EOL; } }
function swp_cache_rebuild() { // Gain access to the database global $wpdb; // Fetch the Post ID $post_id = $_POST['post_id']; // Ensure that the cache for this post is actually expired if (swp_is_cache_fresh($post_id, true, true) == false) { // Force the cache trigger on $_GET['swp_cache'] = 'rebuild'; // Fetch new shares $shares = get_social_warfare_shares($post_id); // Update Bitly links foreach ($shares as $key => $value) { swp_process_url(get_permalink($post_id), $key, $post_id); } // Update the Pinterest image $array['imageID'] = get_post_meta($post_id, 'nc_pinterestImage', true); if ($array['imageID']) { $array['imageURL'] = wp_get_attachment_url($array['imageID']); delete_post_meta($post_id, 'swp_pinterest_image_url'); update_post_meta($post_id, 'swp_pinterest_image_url', $array['imageURL']); } // Update the Twitter username $user_twitter_handle = get_the_author_meta('swp_twitter', swp_get_author($post_id)); if ($user_twitter_handle) { delete_post_meta($post_id, 'swp_twitter_username'); update_post_meta($post_id, 'swp_twitter_username', $user_twitter_handle); } else { delete_post_meta($post_id, 'swp_twitter_username'); } // Update the cache timestamp delete_post_meta($post_id, 'swp_cache_timestamp'); update_post_meta($post_id, 'swp_cache_timestamp', floor(date('U') / 60 / 60)); // Return the share count wp_send_json($shares); } // Kill off all the WordPress functions wp_die(); }