/** * Get the Twitter handle for tweet counts. * * @param string $fallback A default value to fall back to. * @return string $twitter_handle The twitter handle. */ function _swp_get_twitter_handle($fallback = false) { // Fetch the Twitter handle for the Post Author if it exists. if (isset($_GET['post'])) { $user_id = swp_get_author(absint($_GET['post'])); } else { $user_id = get_current_user_id(); } $twitter_handle = get_the_author_meta('swp_twitter', $user_id); if (!$twitter_handle) { $twitter_handle = $fallback; } return $twitter_handle; }
function swp_register_meta_boxes($meta_boxes) { // Setup the prefix to avoid conflicts $prefix = 'nc_'; $options = swp_get_user_options(); $postTypes = swp_get_post_types(); foreach ($postTypes as $key => $value) { $postType[] = $key; } $postType[] = 'page'; $postType[] = 'post'; /***************************************************************** FETCH THE TWITTER HANDLE FOR TWEET COUNTS ******************************************************************/ // Fetch the Twitter handle for the Post Author if it exists if (isset($_GET['post'])) { $post_id = $_GET['post']; $author_id = swp_get_author($post_id); $twitter_handle = get_the_author_meta('swp_twitter', $author_id); // Fetch the Twitter handle for the logged in user if the above fails } else { $logged_in_user = get_current_user_id(); $twitter_handle = get_the_author_meta('swp_twitter', $logged_in_user); } // Fetch the site-wide Twitter Handle if both of the above fail if (!$twitter_handle) { $twitter_handle = $options['twitterID']; } /***************************************************************** BUILD THE OPTIONS FIELDS ******************************************************************/ // Setup our meta box using an array $meta_boxes[0] = array('id' => 'socialWarfare', 'title' => __('Social Warfare Custom Options', 'social-warfare'), 'pages' => $postType, 'context' => 'normal', 'priority' => 'high', 'fields' => array(array('name' => '<span class="dashicons dashicons-share"></span> ' . __('Social Media Image', 'social-warfare'), 'desc' => __('Add an image that is optimized for maximum exposure on Facebook, Google+ and LinkedIn. We recommend 1,200px by 628px.', 'social-warfare'), 'id' => $prefix . 'ogImage', 'type' => 'image_advanced', 'clone' => false, 'class' => $prefix . 'ogImageWrapper', 'max_file_uploads' => 1), array('name' => '<span class="dashicons dashicons-share"></span> ' . __('Social Media Title', 'social-warfare'), 'desc' => __('Add a title that will populate the open graph meta tag which will be used when users share your content onto Facebook, LinkedIn, and Google+. If nothing is provided here, we will use the post title as a backup.', 'social-warfare'), 'id' => $prefix . 'ogTitle', 'type' => 'textarea', 'class' => $prefix . 'ogTitleWrapper', 'clone' => false), array('name' => '<span class="dashicons dashicons-share"></span> ' . __('Social Media Description', 'social-warfare'), 'desc' => __('Add a description that will populate the open graph meta tag which will be used when users share your content onto Facebook, LinkedIn, and Google Plus.', 'social-warfare'), 'id' => $prefix . 'ogDescription', 'class' => $prefix . 'ogDescriptionWrapper', 'type' => 'textarea', 'clone' => false), array('name' => 'divider', 'id' => 'divider', 'type' => 'divider'), array('name' => '<i class="sw sw-pinterest"></i> ' . __('Pinterest Image', 'social-warfare'), 'desc' => __('Add an image that is optimized for maximum exposure on Pinterest. We recommend using an image that is formatted in a 2:3 aspect ratio like 735x1102.', 'social-warfare'), 'id' => $prefix . 'pinterestImage', 'class' => $prefix . 'pinterestImageWrapper', 'type' => 'image_advanced', 'clone' => false, 'max_file_uploads' => 1), array('name' => '<i class="sw sw-pinterest"></i>' . __('Pinterest Description', 'social-warfare'), 'desc' => __('Craft a customized description that will be used when this post is shared on Pinterest. Leave this blank to use the title of the post.', 'social-warfare'), 'id' => $prefix . 'pinterestDescription', 'class' => $prefix . 'pinterestDescriptionWrapper', 'type' => 'textarea', 'clone' => false), array('name' => '<i class="sw sw-twitter"></i> ' . __('Custom Tweet', 'social-warfare'), 'desc' => $options['twitterID'] ? sprintf(__('If this is left blank your post title will be used. Based on your username (@%1$s), <span class="tweetLinkSection">a link being added,</span> and the current content above, your tweet has %2$s characters remaining.', 'social-warfare'), str_replace('@', '', $twitter_handle), '<span class="counterNumber">140</span>') : sprintf(__('If this is left blank your post title will be used. <span ="tweetLinkSection">Based on a link being added, and</span> the current content above, your tweet has %s characters remaining.', 'social-warfare'), '<span class="counterNumber">140</span>'), 'id' => $prefix . 'customTweet', 'class' => $prefix . 'customTweetWrapper', 'type' => 'textarea', 'clone' => false), array('name' => '<span class="dashicons dashicons-randomize"></span> ' . __('Horizontal Buttons Location', 'social-warfare'), 'id' => $prefix . 'postLocation', 'class' => $prefix . 'postLocationWrapper', 'type' => 'select', 'options' => array('default' => __('Default', 'social-warfare'), 'above' => __('Above the Content', 'social-warfare'), 'below' => __('Below the Content', 'social-warfare'), 'both' => __('Both Above and Below the Content', 'social-warfare'), 'none' => __('None/Manual Placement', 'social-warfare')), 'clone' => false, 'std' => 'default'), array('name' => '<span class="dashicons dashicons-randomize"></span> ' . __('Side Floating Buttons Location', 'social-warfare'), 'id' => $prefix . 'floatLocation', 'class' => $prefix . 'floatLocationWrapper', 'type' => 'select', 'options' => array('default' => __('Default', 'social-warfare'), 'on' => __('On', 'social-warfare'), 'off' => __('Off', 'social-warfare')), 'clone' => false, 'std' => 'default'), array('name' => 'divider2', 'id' => 'divider2', 'type' => 'divider'), array('name' => $twitter_handle, 'id' => 'twitterID', 'class' => 'twitterIDWrapper', 'type' => 'hidden', 'std' => $twitter_handle), array('name' => is_swp_registered() ? 'true' : 'false', 'id' => is_swp_registered() ? 'true' : 'false', 'class' => 'registrationWrapper', 'type' => 'hidden', 'std' => is_swp_registered() ? 'true' : 'false'))); // Return the meta boxes return $meta_boxes; }
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_open_graph_tags($info) { // We only modify the Open Graph tags on single blog post pages if (is_singular()) { // If Yoast Open Graph is activated, we only output Open Graph tags if the user has filled out at least one field // Then we'll work along with Yoast to make sure all fields get filled properly if (defined('WPSEO_VERSION')) { global $wpseo_og; $yoast_og_setting = has_action('wpseo_head', array($wpseo_og, 'opengraph')); } else { $yoast_og_setting = false; } if (isset($info['title']) && $info['title'] || isset($info['description']) && $info['description'] || isset($info['imageURL']) && $info['imageURL'] || !$yoast_og_setting) { /***************************************************************** * * * YOAST SEO: It rocks, so let's coordinate with it * * * ******************************************************************/ // Check if Yoast Exists so we can coordinate output with their plugin accordingly if (defined('WPSEO_VERSION')) { // Collect their Social Descriptions as backups if they're not defined in ours $yoast_og_title = get_post_meta($info['postID'], '_yoast_wpseo_opengraph-title', true); $yoast_og_description = get_post_meta($info['postID'], '_yoast_wpseo_opengraph-description', true); $yoast_og_image = get_post_meta($info['postID'], '_yoast_wpseo_opengraph-image', true); // Collect their SEO fields as 3rd string backups in case we need them $yoast_seo_title = get_post_meta($info['postID'], '_yoast_wpseo_title', true); $yoast_seo_description = get_post_meta($info['postID'], '_yoast_wpseo_metadesc', true); // Cancel their output if ours have been defined so we don't have two sets of tags global $wpseo_og; remove_action('wpseo_head', array($wpseo_og, 'opengraph'), 30); // Fetch the WPSEO_SOCIAL Values $wpseo_social = get_option('wpseo_social'); } // Add all our Open Graph Tags to the Return Header Output $info['header_output'] .= PHP_EOL . '<meta property="og:type" content="article" /> '; /***************************************************************** * * * JETPACK: If ours are enabled, disable theirs * * * ******************************************************************/ if (class_exists('JetPack')) { add_filter('jetpack_enable_opengraph', '__return_false', 99); add_filter('jetpack_enable_open_graph', '__return_false', 99); } /***************************************************************** * * * OPEN GRAPH TITLE * * * ******************************************************************/ // Open Graph Title: Create an open graph title meta tag if ($info['title']) { // If the user defined an social media title, let's use it. $info['header_output'] .= PHP_EOL . '<meta property="og:title" content="' . $info['title'] . '" />'; } elseif (isset($yoast_og_title) && $yoast_og_title) { // If the user defined an title over in Yoast, let's use it. $info['header_output'] .= PHP_EOL . '<meta property="og:title" content="' . trim($yoast_og_title) . '" />'; } elseif (isset($yoast_seo_title) && $yoast_seo_title) { // If the user defined an title over in Yoast, let's use it. $info['header_output'] .= PHP_EOL . '<meta property="og:title" content="' . trim($yoast_seo_title) . '" />'; } else { // If nothing else is defined, let's use the post title $info['header_output'] .= PHP_EOL . '<meta property="og:title" content="' . trim(convert_smart_quotes(htmlspecialchars_decode(get_the_title()))) . '" />'; } /***************************************************************** * * * OPEN GRAPH DESCRIPTION * * * ******************************************************************/ // Open Graph Description: Create an open graph description meta tag if ($info['description']) { // If the user defined an social media description, let's use it. $info['header_output'] .= PHP_EOL . '<meta property="og:description" content="' . $info['description'] . '" />'; } elseif (isset($yoast_og_description) && $yoast_og_description) { // If the user defined an description over in Yoast, let's use it. $info['header_output'] .= PHP_EOL . '<meta property="og:description" content="' . $yoast_og_description . '" />'; } elseif (isset($yoast_seo_description) && $yoast_seo_description) { // If the user defined an description over in Yoast, let's use it. $info['header_output'] .= PHP_EOL . '<meta property="og:description" content="' . $yoast_seo_description . '" />'; } else { // If nothing else is defined, let's use the post excerpt $info['header_output'] .= PHP_EOL . '<meta property="og:description" content="' . convert_smart_quotes(htmlspecialchars_decode(swp_get_excerpt_by_id($info['postID']))) . '" />'; } /***************************************************************** * * * OPEN GRAPH IMAGE * * * ******************************************************************/ // Open Graph Image: Create an open graph image meta tag if ($info['imageURL']) { // If the user defined an image, let's use it. $info['header_output'] .= PHP_EOL . '<meta property="og:image" content="' . $info['imageURL'] . '" />'; } elseif (isset($yoast_og_image) && $yoast_og_image) { // If the user defined an image over in Yoast, let's use it. $info['header_output'] .= PHP_EOL . '<meta property="og:image" content="' . $yoast_og_image . '" />'; } else { // If nothing else is defined, let's use the post Thumbnail as long as we have the URL cached $og_image = get_post_meta($info['postID'], 'swp_open_thumbnail_url', true); if ($og_image) { $info['header_output'] .= PHP_EOL . '<meta property="og:image" content="' . $og_image . '" />'; } } /***************************************************************** * * * OPEN GRAPH IMAGE DIMENSIONS * * * ******************************************************************/ if ($info['image_data']) { $info['header_output'] .= PHP_EOL . '<meta property="og:image:width" content="' . $info['image_data'][1] . '" />'; $info['header_output'] .= PHP_EOL . '<meta property="og:image:height" content="' . $info['image_data'][2] . '" />'; } elseif (get_post_meta($info['postID'], 'swp_open_graph_image_data', true)) { $info['image_data'] = json_decode(get_post_meta($info['postID'], 'swp_open_graph_image_data', true)); $info['header_output'] .= PHP_EOL . '<meta property="og:image:width" content="' . $info['image_data'][1] . '" />'; $info['header_output'] .= PHP_EOL . '<meta property="og:image:height" content="' . $info['image_data'][2] . '" />'; } /***************************************************************** * * * OPEN GRAPH URL & Site Name * * * ******************************************************************/ $info['header_output'] .= PHP_EOL . '<meta property="og:url" content="' . get_permalink() . '" />'; $info['header_output'] .= PHP_EOL . '<meta property="og:site_name" content="' . get_bloginfo('name') . '" />'; /***************************************************************** * * * OPEN GRAPH AUTHOR * * * ******************************************************************/ // Add the Facebook Author URL if (get_the_author_meta('swp_fb_author', swp_get_author($info['postID']))) { // Output the Facebook Author URL $facebook_author = get_the_author_meta('swp_fb_author', swp_get_author($info['postID'])); $info['header_output'] .= PHP_EOL . '<meta property="article:author" content="' . $facebook_author . '" />'; } elseif (get_the_author_meta('facebook', swp_get_author($info['postID'])) && defined('WPSEO_VERSION')) { // Output the Facebook Author URL $facebook_author = get_the_author_meta('facebook', swp_get_author($info['postID'])); $info['header_output'] .= PHP_EOL . '<meta property="article:author" content="' . $facebook_author . '" />'; } /***************************************************************** * * * OPEN GRAPH PUBLISHER * * * ******************************************************************/ // If they have a Facebook Publisher URL in our settings... if (isset($info['swp_user_options']['facebookPublisherUrl']) && $info['swp_user_options']['facebookPublisherUrl'] != '') { // Output the Publisher URL $info['header_output'] .= PHP_EOL . '<meta property="article:publisher" content="' . $info['swp_user_options']['facebookPublisherUrl'] . '" />'; // If they have a Facebook Publisher URL in Yoast's settings... } elseif (isset($wpseo_social) && isset($wpseo_social['facebook_site']) && $wpseo_social['facebook_site'] != '') { // Output the Publisher URL $info['header_output'] .= PHP_EOL . '<meta property="article:publisher" content="' . $wpseo_social['facebook_site'] . '" />'; } $info['header_output'] .= PHP_EOL . '<meta property="article:published_time" content="' . get_post_time('c') . '" />'; $info['header_output'] .= PHP_EOL . '<meta property="article:modified_time" content="' . get_post_modified_time('c') . '" />'; $info['header_output'] .= PHP_EOL . '<meta property="og:updated_time" content="' . get_post_modified_time('c') . '" />'; /***************************************************************** * * * OPEN GRAPH APP ID * * * ******************************************************************/ // If the Facebook APP ID is in our settings... if (isset($info['swp_user_options']['facebookAppID']) && $info['swp_user_options']['facebookAppID'] != '') { // Output the Facebook APP ID $info['header_output'] .= PHP_EOL . '<meta property="fb:app_id" content="' . $info['swp_user_options']['facebookAppID'] . '" />'; // If the Facebook APP ID is set in Yoast's settings... } elseif (isset($wpseo_social) && isset($wpseo_social['fbadminapp']) && $wpseo_social['fbadminapp'] != '') { // Output the Facebook APP ID $info['header_output'] .= PHP_EOL . '<meta property="fb:app_id" content="' . $wpseo_social['fbadminapp'] . '" />'; } else { // Output the Facebook APP ID $info['header_output'] .= PHP_EOL . '<meta property="fb:app_id" content="529576650555031" />'; } } } // Return the variable containing our information for the meta tags return $info; }
/** * Rebuild the share cache. * * @since unkown * @global $wpdb * @return void */ function swp_cache_rebuild() { global $wpdb; $post_id = absint($_POST['post_id']); // Bail if we already have fresh cache. if (swp_is_cache_fresh($post_id, true, true)) { wp_send_json_error(); die; } // Force the cache trigger on. set_query_var('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(); }
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(); }