Location: PHPKode > scripts > MP Share Center > mp-share-center/mp-share-center.php
<?php
/*
Plugin Name: MP Share Center
Plugin URI: http://MikesPickzWS.com/wordpress-plugins/mp-share-center/
Description: Add a convenient Social Sharing Toolbar above and/or below the post with the MP Share Center. This will add Facebook Like, Twitter Tweet, Google Plus, LinkedIn Share and StumbleUpon Stumble buttons and boxes to your content so readers can easily share your content. You can add a custom "Call to Action" to ask your users to share via the easy options page.
Version: 2.2
Author: MikesPickz Web Solutions, Inc.
Author URI: http://MikesPickzWS.com
License: GPL2
*/

/*  Copyright 2011  MikesPickz Web Solutions, Inc.  (email : hide@address.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as 
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
//Initiate Options
function mp_share_center_init() { 
	register_setting('mp_sc_options', 'mp_share_center_options', 'mp_share_center_options_validate');
}
add_action('admin_init', 'mp_share_center_init');

//Create Settings Page
function mp_share_center_add_page() {
	add_options_page('MP Share Center Options', 'MP Share Center', 'manage_options',  __FILE__, 'mp_share_center_options_page');
}
add_action('admin_menu', 'mp_share_center_add_page');

//Actual Content of Settings Page
function mp_share_center_options_page() {
	if (!current_user_can('manage_options'))  {
		wp_die( __('You do not have sufficient permissions to access this page.') );
	} ?>
    <div class="wrap">
    <div class="icon32" id="icon-tools"><br /></div>
    <h2>MP Share Center Options</h2>
    <h4>Brought to you by <a href="http://MikesPickzWS.com/" target="_blank">MikesPickz Web Solutions, Inc.</a> | <a href="http://MikesPickzWS.com/wordpress-plugins/mp-share-center/" target="_blank">Support Site</a></h4>
    <form method="post" action="options.php">
	<?php settings_fields('mp_sc_options'); ?>
    <?php $options = get_option('mp_share_center_options'); ?>
<table class="form-table">
<tr><td colspan="2"><strong>Which page-types should the MP Share Center be displayed on?</strong></td></tr>
<tr valign="top">
<td align="right">Front Page:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_front]" value="1" <?php checked('1', $options['mp_share_display_front']); ?> /> (This is specified in Settings -> Reading)</td>
</tr>
<tr valign="top">
<td align="right">Main Blog Page:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_home]" value="1" <?php checked('1', $options['mp_share_display_home']); ?> /> (If Front Page is a Static Page, this is the page specified 'Posts Page' in Settings -> Reading)</td>
</tr>
<tr valign="top">
<td align="right">Post:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_post]" value="1" <?php checked('1', $options['mp_share_display_post']); ?> /></td>
</tr>
<tr valign="top">
<td align="right">Page:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_page]" value="1" <?php checked('1', $options['mp_share_display_page']); ?> /></td>
</tr>
<tr valign="top">
<td align="right">Archive:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_archive]" value="1" <?php checked('1', $options['mp_share_display_archive']); ?> /></td>
</tr>
<tr valign="top">
<td align="right">Search:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_search]" value="1" <?php checked('1', $options['mp_share_display_search']); ?> /></td>
</tr>

<tr><td colspan="2"><strong>Which Social Networks should be displayed?</strong></td></tr>
<tr valign="top">
<td align="right">Facebook Like:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_facebook]" value="1" <?php checked('1', $options['mp_share_display_facebook']); ?> /></td>
</tr>
<tr valign="top">
<td align="right">Twitter:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_twitter]" value="1" <?php checked('1', $options['mp_share_display_twitter']); ?> /></td>
</tr>
<tr valign="top">
<td align="right">Google Plus:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_google]" value="1" <?php checked('1', $options['mp_share_display_google']); ?> /></td>
</tr>
<tr valign="top">
<td align="right">LinkedIn:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_linkedin]" value="1" <?php checked('1', $options['mp_share_display_linkedin']); ?> /></td>
</tr>
<tr valign="top">
<td align="right">StumbleUpon:</td><td><input type="checkbox" name="mp_share_center_options[mp_share_display_stumble]" value="1" <?php checked('1', $options['mp_share_display_stumble']); ?> /></td>
</tr>

<tr><td colspan="2"><strong>Where should the MP Share Center be located?</strong></td></tr>
<tr valign="top">
<td>Above Post: <br />
<input type="radio" name="mp_share_center_options[mp_share_location_top]" value="buttons" <?php checked('buttons', $options['mp_share_location_top']); ?>> Horizontal Buttons <br />
<input type="radio" name="mp_share_center_options[mp_share_location_top]" value="boxes" <?php checked('boxes', $options['mp_share_location_top']); ?>> Vertical Boxes
<br />
<input type="radio" name="mp_share_center_options[mp_share_location_top]" value="none" <?php checked('none', $options['mp_share_location_top']); ?>> None
</td>
<td>
Below Post: <br />
<input type="radio" name="mp_share_center_options[mp_share_location_below]" value="buttons" <?php checked('buttons', $options['mp_share_location_below']); ?>> Horizontal Buttons <br />
<input type="radio" name="mp_share_center_options[mp_share_location_below]" value="boxes" <?php checked('boxes', $options['mp_share_location_below']); ?>> Vertical Boxes<br />
<input type="radio" name="mp_share_center_options[mp_share_location_below]" value="none" <?php checked('none', $options['mp_share_location_below']); ?>> None
</td>
</tr>
<tr valign="top">
<td>Example Horizontal Button:<br /><img src="http://MikesPickzWS.com/images/horizontal_button.JPG" alt="Horizontal Button" height="24"width="101" /></td>
<td>Example Vertical Box:<br /><img src="http://MikesPickzWS.com/images/vertical_box.JPG" alt="Vertical Box" height="68" width="63" /></td>
</tr>
<tr><td colspan="2"><small>Select if you would like the Share Center to be above the post, below the post or both.</small></td></tr>

<tr valign="top">
<th scope="row"><strong>Share Center Call to Action</strong></th>
<td> <input style="width: 400px;" type="text" name="mp_share_center_options[mp_share_action]" value="<?php echo stripslashes ($options['mp_share_action']); ?>" /></td>
</tr>
<tr><td colspan="2"><small>Text to display above the Share Center (ie: Share and Enjoy). Only displayed below the post.</small></td></tr>

<tr valign="top">
<th scope="row"><strong>Twitter User Name</strong></th>
<td>@<input style="width: 400px;" type="text" name="mp_share_center_options[mp_share_twitter_name]" value="<?php echo stripslashes ($options['mp_share_twitter_name']); ?>" /></td>
</tr>
<tr><td colspan="2"><small>Name to display at the end of Tweets posted with Tweet button</small></td></tr>
</table>
    	<p class="submit">
    	<input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
    	</p>
    </form>
</div>
<?php
}

//Validate the options before database insertion
function mp_share_center_options_validate($input) {
	if ( ! isset( $input['mp_share_display_front'] ) )
		$input['mp_share_display_front'] = null;
		$input['mp_share_display_front'] = ( $input['mp_share_display_front'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_home'] ) )
		$input['mp_share_display_home'] = null;
		$input['mp_share_display_home'] = ( $input['mp_share_display_home'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_post'] ) )
		$input['mp_share_display_post'] = null;
		$input['mp_share_display_post'] = ( $input['mp_share_display_post'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_page'] ) )
		$input['mp_share_display_page'] = null;
		$input['mp_share_display_page'] = ( $input['mp_share_display_page'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_archive'] ) )
		$input['mp_share_display_archive'] = null;
		$input['mp_share_display_archive'] = ( $input['mp_share_display_archive'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_search'] ) )
		$input['mp_share_display_search'] = null;
		$input['mp_share_display_search'] = ( $input['mp_share_display_search'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_facebook'] ) )
		$input['mp_share_display_facebook'] = null;
		$input['mp_share_display_facebook'] = ( $input['mp_share_display_facebook'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_twitter'] ) )
		$input['mp_share_display_twitter'] = null;
		$input['mp_share_display_twitter'] = ( $input['mp_share_display_twitter'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_google'] ) )
		$input['mp_share_display_google'] = null;
		$input['mp_share_display_google'] = ( $input['mp_share_display_google'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_linkedin'] ) )
		$input['mp_share_display_linkedin'] = null;
		$input['mp_share_display_linkedin'] = ( $input['mp_share_display_linkedin'] == 1 ? 1 : 0 );
	if ( ! isset( $input['mp_share_display_stumble'] ) )
		$input['mp_share_display_stumble'] = null;
		$input['mp_share_display_stumble'] = ( $input['mp_share_display_stumble'] == 1 ? 1 : 0 );
	$input['mp_share_location_top'] =  wp_filter_post_kses($input['mp_share_location_top']);
	$input['mp_share_location_below'] =  wp_filter_post_kses($input['mp_share_location_below']);
	$input['mp_share_action'] =  wp_filter_post_kses($input['mp_share_action']);
	$input['mp_share_twitter_name'] =  wp_filter_post_kses($input['mp_share_twitter_name']);

	return $input;
}

//Add Settings Link on Plugins Page
function mp_share_center_add_settings_link($links, $file) {
	static $this_plugin;
	if (!$this_plugin) $this_plugin = plugin_basename(__FILE__);
 
	if ($file == $this_plugin){
		$settings_link = '<a href="admin.php?page=mp-share-center/mp-share-center.php">'.__("Settings").'</a>';
		array_unshift($links, $settings_link);
	}
	return $links;
}
add_filter('plugin_action_links', 'mp_share_center_add_settings_link', 10, 2);

//<head> Includes
function mp_share_center_head_includes() { 
	//Gather user options
	$options = get_option('mp_share_center_options');
	$display_front = $options['mp_share_display_front'];
	$display_home = $options['mp_share_display_home'];
	$display_post = $options['mp_share_display_post'];
	$display_page = $options['mp_share_display_page'];
	$display_archive = $options['mp_share_display_archive'];
	$display_search = $options['mp_share_display_search'];
	$location_below = $options['mp_share_location_below'];
	//The styles to load
	$css1 = "
	<!--MP Share Center Styles-->
	<style>
	.mp-share-buttons, .mp-share-boxes { display: inline; }
	.mp-share-buttons li, .mp-share-boxes li { float: left; list-style-type: none !important; }
	.mp-share-buttons-su { padding-left: 20px; margin-top: 1px; }
	.mp-share-boxes li { width: 100px; }
	.mp-share-clear-fix { clear: both; }
	#mp-share-below-action { font-family: 'Permanent Marker', arial, serif; font-size: 32px; display: block; padding: 5px; }
	</style>\n";
	$css2 = "<link href='http://fonts.googleapis.com/css?family=Permanent+Marker&amp;v1' rel='stylesheet' type='text/css' />";
	//Check what type of page this is
	if ($location_below == 'none' || $location_below == '' ) {
		if (is_admin()) { }
		elseif (is_front_page()) { if ($display_front == 1) { echo $css1; } }
		elseif (is_home()) { if ($display_home == 1) { echo $css1; } }
		elseif (is_single()) { if ($display_post == 1) { echo $css1; } }
		elseif (is_page()) { if ($display_page == 1) { echo $css1; } }
		elseif (is_archive()) { if ($display_archive == 1) { echo $css1; } }
		elseif (is_search()) { if ($display_search == 1) { echo $css1; } }
	} elseif ($location_below == 'buttons' || $location_below == 'boxes') {
		if (is_admin()) { }
		elseif (is_front_page()) { if ($display_front == 1) { echo $css1.$css2; } }
		elseif (is_home()) { if ($display_home == 1) { echo $css1.$css2; } }
		elseif (is_single()) { if ($display_post == 1) { echo $css1.$css2; } }
		elseif (is_page()) { if ($display_page == 1) { echo $css1.$css2; } }
		elseif (is_archive()) { if ($display_archive == 1) { echo $css1.$css2; } }
		elseif (is_search()) { if ($display_search == 1) { echo $css1.$css2; } }
	}
}
add_action('wp_head', 'mp_share_center_head_includes'); 

//Takes the first image in a post and sets it as the thumbnail for the Facebook Like Button
function mp_share_center_fb_like_thumbnails() {
	global $posts;
	$default = '';
	$content = $posts[0]->post_content; // $posts is an array, fetch the first element
	$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
	if ( $output > 0 )
		$thumb = $matches[1][0];
	else
		$thumb = $default;

	echo "\n<link rel=\"image_src\" href=\"$thumb\" /><!-- Facebook Like Thumbnail -->\n";
}
add_action( 'wp_head', 'mp_share_center_fb_like_thumbnails' );

//Footer Includes
function mp_share_center_footer_includes() {
	//Gather user options
	$options = get_option('mp_share_center_options');
	$display_front = $options['mp_share_display_front'];
	$display_home = $options['mp_share_display_home'];
	$display_post = $options['mp_share_display_post'];
	$display_page = $options['mp_share_display_page'];
	$display_archive = $options['mp_share_display_archive'];
	$display_search = $options['mp_share_display_search'];
	$display_facebook = $options['mp_share_display_facebook'];
	$display_twitter = $options['mp_share_display_twitter'];
	$display_google = $options['mp_share_display_google'];
	$display_linkedin = $options['mp_share_display_linkedin'];
	$display_stumble = $options['mp_share_display_stumble'];
	//The scripts to load
	$a = '<!--MP Share Center Scripts-->';
	if ($display_twitter == 1) {
		$a .= '<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><!--Twitter Tweet Button-->';
	}
	if ($display_google == 1) {
		$a .= '<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script><!--Google +1 Button-->';
	}
	if ($display_linkedin == 1) {
		$a .= '<script type="text/javascript" src="http://platform.linkedin.com/in.js"></script><!--LinkedIn Share Button-->';
	}
	//Check what type of page this is
	if (is_admin()) { }
	elseif (is_front_page()) { if ($display_front == 1) { echo $a; } }
	elseif (is_home()) { if ($display_home == 1) { echo $a; } }
	elseif (is_single()) { if ($display_post == 1) { echo $a; } }
	elseif (is_page()) { if ($display_page == 1) { echo $a; } }
	elseif (is_archive()) { if ($display_archive == 1) { echo $a; } }
	elseif (is_search()) { if ($display_search == 1) { echo $a; } }
}
add_action('wp_footer', 'mp_share_center_footer_includes');

//Add the Share Center to the Content
function mp_share_center_insert_content($content) { 
	//Gather user options
	$options = get_option('mp_share_center_options');
	$display_front = $options['mp_share_display_front'];
	$display_home = $options['mp_share_display_home'];
	$display_post = $options['mp_share_display_post'];
	$display_page = $options['mp_share_display_page'];
	$display_archive = $options['mp_share_display_archive'];
	$display_search = $options['mp_share_display_search'];
	$display_facebook = $options['mp_share_display_facebook'];
	$display_twitter = $options['mp_share_display_twitter'];
	$display_google = $options['mp_share_display_google'];
	$display_linkedin = $options['mp_share_display_linkedin'];
	$display_stumble = $options['mp_share_display_stumble'];
	$location_top = $options['mp_share_location_top'];
	$location_below = $options['mp_share_location_below'];
	$share = stripslashes ($options['mp_share_action']);
	$twitter = stripslashes ($options['mp_share_twitter_name']);
	//Gather Post Link Information
	$long_link = urlencode(get_permalink($post->ID));
	$short_link = urlencode(wp_get_shortlink(get_the_ID()));
		
	//Buttons
	$buttons = "<ul class='mp-share-buttons'>";
	if ($display_facebook == 1) {
	$buttons .= "<li class='mp-share-buttons-fb'><iframe src='http://www.facebook.com/plugins/like.php?href=$long_link&amp;send=false&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:100px; height:21px;' allowTransparency='true'></iframe></li>";
	}
	if ($display_twitter == 1) {
	$buttons .= "<li class='mp-share-buttons-tw'><a href='http://twitter.com/share?url=$short_link&amp;counturl=$long_link' class='twitter-share-button' data-count='horizontal' data-via='$twitter'></a></li>";
	}
	if ($display_google == 1) {
	$buttons .= "<li class='mp-share-buttons-gp'><g:plusone href='$long_link' size='medium'></g:plusone></li>";
	}
	if ($display_linkedin == 1) {
	$buttons .= "<li class='mp-share-buttons-li'><script type='in/share' data-url='$long_link' data-counter='right'></script></li>";
	}
	if ($display_stumble == 1) {
	$buttons .= "<li class='mp-share-buttons-su'><script src='http://www.stumbleupon.com/hostedbadge.php?s=1&r=$long_link'></script></li>";
	}
	$buttons .= "</ul>";
	$buttons .= "<div class='mp-share-clear-fix'></div>";
	
	//Boxes
	$boxes = "<ul class='mp-share-boxes'>";
	if ($display_facebook == 1) {
	$boxes .= "<li class='mp-share-boxes-fb'><iframe src='http://www.facebook.com/plugins/like.php?href=$long_link&amp;send=false&amp;layout=box_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=arial&amp;height=21' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:100px; height:90px;' allowTransparency='true'></iframe></li>";
	}
	if ($display_twitter == 1) {
	$boxes .= "<li class='mp-share-boxes-tw'><a href='http://twitter.com/share?url=$short_link&amp;counturl=$long_link' class='twitter-share-button' data-count='vertical' data-via='$twitter'></a></li>";
	}
	if ($display_google == 1) {
	$boxes .= "<li class='mp-share-boxes-gp'><g:plusone href='$long_link' size='tall'></g:plusone></li>";
	}
	if ($display_linkedin == 1) {
	$boxes .= "<li class='mp-share-boxes-li'><script type='in/share' data-url='$long_link' data-counter='top'></script></li>";
	}
	if ($display_stumble == 1) {
	$boxes .= "<li class='mp-share-boxes-su'><script src='http://www.stumbleupon.com/hostedbadge.php?s=5&r=$long_link'></script></li>";
	}
	$boxes .= "</ul>";
	$boxes .= "<div class='mp-share-clear-fix'></div>";
	
	//Call to Action
	$action = "<span id='mp-share-below-action'>$share</span>";
	
	//Check what type of page this is
	if (is_admin()) { return $content; }
	elseif (is_front_page()) { if ($display_front == 0) { return $content; } }
	elseif (is_home()) { if ($display_home == 0) { return $content; } }
	elseif (is_single()) { if ($display_post == 0) { return $content; } }
	elseif (is_page()) { if ($display_page == 0) { return $content; } }
	elseif (is_archive()) { if ($display_archive == 0) { return $content; } }
	elseif (is_search()) { if ($display_search == 0) { return $content; } }
	
	//If the user has selected to show Share Center, where?
	if ($location_top == 'none' && $location_below == 'none') { return $content; } 
	elseif ($location_top == 'buttons' && $location_below == 'none') { return $buttons.$content; } 
	elseif ($location_top == 'buttons'  && $location_below == 'buttons') { return $buttons.$content.$action.$buttons; } 
	elseif ($location_top == 'buttons'  && $location_below == 'boxes') { return $buttons.$content.$action.$boxes; } 
	elseif ($location_top == 'boxes' && $location_below == 'none') { return $boxes.$content; } 
	elseif ($location_top == 'boxes'  && $location_below == 'buttons') { return $boxes.$content.$action.$buttons; } 
	elseif ($location_top == 'boxes'  && $location_below == 'boxes') { return $boxes.$content.$action.$boxes; }
	elseif ($location_top == 'none'  && $location_below == 'buttons') { return $content.$action.$buttons; } 
	elseif ($location_top == 'none'  && $location_below == 'boxes') { return $content.$action.$boxes; }
	
return $content;
}
add_filter('the_content', 'mp_share_center_insert_content', 25);

//Delete Database fields on uninstall
function mp_share_center_remove() {
	delete_option('mp_share_center_options');
}
register_uninstall_hook( __FILE__, 'mp_share_center_remove');
?>
Return current item: MP Share Center