<?php

/*
Plugin Name: Add Custom Share Image
Description: Don't let Facebook pick your share image.  Plugin for adding a custom share image into posts.  When someone shares on Facebook or hits the Like button, the image you have added will be the one that is used.
Version: 0.4.1
*/

/* Copyright (C) 2011

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

 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
 add_filter('site_transient_update_plugins', 'dd_remove_update_nag');
function dd_remove_update_nag($value) {
 unset($value->response[ plugin_basename(__FILE__) ]);
 return $value;
}

 class Add_Your_Own_Headers {
 	
 	var $version = "0.4.1";

	function Add_Your_Own_Headers() {
		global $wp_version;
		$this->wp_version = $wp_version;
	}

	function init() {
		if (function_exists('load_plugin_textdomain')) {
			load_plugin_textdomain('add_your_own_headers', 'wp-content/plugins/add-your-own-headers');
		}
	}

	 function wp_head() {
		global $post;
		$meta_string = null;
		
		if (is_single() || is_page()) {
			echo "\n<!-- add_your_own_headers $this->version -->\n";
		
			// custom tags
        		$custom_001 = stripslashes(get_post_meta($post->ID, "custom_header_001", true));
        		$custom_002 = stripslashes(get_post_meta($post->ID, "custom_header_002", true));
			if (isset ($custom_001) && !empty($custom_001)) {
				$meta_string .= sprintf("%s\n", $custom_001);
			}
			if (isset ($custom_002) && !empty($custom_002)) {
				$meta_string .= sprintf("%s\n", $custom_002);
			}

			if ($meta_string != null) {
				echo $meta_string;
			}
		}
	}
	
	function add_headers_textinput() {
	    global $post;
	    $custom_tag_001 = htmlspecialchars(get_post_meta($post->ID, 'custom_header_001', true));
	    $custom_tag_002 = htmlspecialchars(get_post_meta($post->ID, 'custom_header_002', true));
		?>

		<?php if (substr($this->wp_version, 0, 3) == '2.5') { ?>
		<div id="postayoh" class="postbox closed">
		<h3><?php _e('Add Custom Share Image', 'add_your_own_headers') ?></h3>
		<?php } else { ?>
		<?php } ?>
		<h3 class="dbx-handle"><img class="alignleft size-full wp-image-2413" title="wordpress_to_facebook" src="http://mrinternettips.com/wp-content/uploads/2011/07/wordpress_to_facebook.jpg" alt="" width="144" height="134" /><?php _e('Add Custom Share Image', 'add_your_own_headers') ?></h3>
		<form name="dofollow" action="" method="post">
		<input value="ayoh_edit" type="hidden" name="ayoh_edit" />
		<table style="margin-bottom:40px; margin-top:30px; cellpadding="15" cellspacing="15"">
		<tr>
		<th scope="row" style="text-align:right;"><?php _e('') ?></th>
		<td><input value="<?php echo $custom_tag_001 ?>" type="text" name="custom_header_001" size="50"/><a onclick="return false;" title="Add an Image" class="thickbox button" id="add_image" href="media-upload.php?post_id=2352&amp;type=image&amp;TB_iframe=true&amp;width=640&amp;height=324">Get Image</a><br/><span style="font-weight: bold;">Follow the steps below</span><br>
<ol>
  <li>Paste &lt;link rel=&quot;image_src&quot; href=&quot;PUT_IMAGE_URL_HERE&quot; /&gt; in
the box above</li>
  <li>Click on get image above</li>
  <li>Upload image and get the image path</li>
  <li>Paste your image path above where it says "PUT_IMAGE_URL_HERE"</li>
  <li>Publish your post or page</li>
</ol>
</td>
      <td><h3>SEE HOW TO DO IT BELOW</h3><object width="299" height="170"><param name="movie" value="http://www.youtube.com/v/nhRLSWYHQX8?version=3&amp;hl=en_US&amp;rel=0&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/nhRLSWYHQX8?version=3&amp;hl=en_US&amp;rel=0&amp;hd=1" type="application/x-shockwave-flash" width="299" height="170" allowscriptaccess="always" allowfullscreen="true"></embed></object></td>

		</tr>
		
		</table>
		</form>

		<?php if (substr($this->wp_version, 0, 3) == '2.5') { ?>
		</div>
		<?php } else { ?>
		<?php } ?>

		<?php
	}

	function post_headers($id) {
	    $ayoh_edit = $_POST["ayoh_edit"];
	    if (isset($ayoh_edit) && !empty($ayoh_edit)) {
		    $custom_tag_001 = stripslashes($_POST["custom_header_001"]);
		    $custom_tag_002 = stripslashes($_POST["custom_header_002"]);

		    delete_post_meta($id, 'custom_header_001');
		    delete_post_meta($id, 'custom_header_002');

		    if (isset($custom_tag_001) && !empty($custom_tag_001)) {
			    add_post_meta($id, 'custom_header_001', $custom_tag_001);
		    }
		    if (isset($custom_tag_002) && !empty($custom_tag_002)) {
			    add_post_meta($id, 'custom_header_002', $custom_tag_002);
		    }
	    }
	}

	// not used at the moment ...
	function admin_menu() {
		add_submenu_page('options-general.php', __('Add your own headers'), __('Add your own headers'), 5, __FILE__, array($this, 'plugin_menu'));
	}
			
			function plugin_menu() {
				$message = null;
				$message_updated = __("Settings updated.");
				
				// update options
				if ($_POST['action'] && $_POST['action'] == 'update_ayoh_settings') {
					$message = $message_updated;
					update_option('ayoh_headers_on_homepage', $_POST['ayoh_headers_on_homepage']);
					wp_cache_flush();
				}
				
			?>

<?php
	
	} // plugin_menu

} // Add_Your_Own_Headers

$_ayoh = new Add_Your_Own_Headers();

//add_option("ayoh_headers_on_homepage", null, __('Show headers on hommepage'), 'yes');

add_action('init', array($_ayoh, 'init'));
add_action('wp_head', array($_ayoh, 'wp_head'));

//add_action('admin_menu', array($_ayoh, 'admin_menu'));

add_action('simple_edit_form', array($_ayoh, 'add_headers_textinput'));
add_action('edit_form_advanced', array($_ayoh, 'add_headers_textinput'));
add_action('edit_page_form', array($_ayoh, 'add_headers_textinput'));

add_action('edit_post', array($_ayoh, 'post_headers'));
add_action('publish_post', array($_ayoh, 'post_headers'));
add_action('save_post', array($_ayoh, 'post_headers'));
add_action('edit_page_form', array($_ayoh, 'post_headers'));

?>
