<?php
/*
Plugin Name: Delete postmeta test plugin
Plugin URI: http://www.studiograsshopper.ch/dynamic-content-gallery/
Version: 0.1
Author: Ade Walker, Studiograsshopper
Author URI: http://www.studiograsshopper.ch
Description: To demonstrate problems with WP 3.0 and handling of postmeta deletion via Post editor metaboxes hooked to save_post
*/

/*  Copyright 2010  Ade WALKER  (email : info@studiograsshopper.ch) */

/*	License information
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License 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.

The license for this software can be found here: 
http://www.gnu.org/licenses/gpl-2.0.html
*/

/* Prevent direct access to this file */
if (!defined('ABSPATH')) {
	exit("Sorry, you are not allowed to access this file directly.");
}

define( 'SGR_FILE_HOOK', 'postmeta_test_plugin' );

/* Admin - Adds Metaboxes to Post/Page Editor */
// Function defined in dfcg-admin-metaboxes.php
add_action('admin_menu', 'sgr_add_metabox');

/* Admin - Saves Metabox data in Post/Page Editor */
// Function defined in dfcg-admin-metaboxes.php
add_action('save_post', 'sgr_save_metabox_data', 1, 2);


/**
* Adds metaboxes to Post and Page screen
*
* Hooked to 'admin_menu'
*
*/
function sgr_add_metabox() {

		add_meta_box( SGR_FILE_HOOK . '_box', __( 'Test metabox' ), 'sgr_meta_box', 'page', 'side', 'low' );
	
		add_meta_box( SGR_FILE_HOOK . '_box', __( 'Test metabox' ), 'sgr_meta_box', 'post', 'side', 'low' );
}


/**
* Populates metaboxes in Post and Page screen
*
* Called by add_meta_box() in dfcg_add_metabox() function
*
* Note: Markup follows WP standards for Post/Page Editor sidebar
*
*/
function sgr_meta_box($post) {

	// Use nonce for verification
	echo '<input type="hidden" name="sgr_metabox_noncename" id="sgr_metabox_noncename" value="' . 
	wp_create_nonce( SGR_FILE_HOOK ) . '" />';
	
	?>
	
<?php /* IMAGE BLOCK */ ?>
	<div class="sgr-form">
		<h5><?php _e('Enter a URL to something'); ?>:</h5>
		<label class="screen-reader-text" for="_sgr-image"><?php _e('Image URL', DFCG_DOMAIN); ?></label>
		<textarea id="_sgr-image" name="_sgr-image" style="font-size:11px;" cols="38" rows="2"><?php echo get_post_meta($post->ID, '_sgr-image', true); ?></textarea>
		
	</div>
	
<?php /* DESC BLOCK */ ?>
	
	<div class="sgr-form">
		<h5><?php _e('Enter some text'); ?>:</h5>
		<label class="screen-reader-text" for="_sgr-desc"><?php _e('Text', DFCG_DOMAIN); ?></label>
		<textarea id="_sgr-desc" name="_sgr-desc" style="font-size:11px;" cols="38" rows="4"><?php echo get_post_meta($post->ID, '_sgr-desc', true); ?></textarea>
	</div>
<?php
}


/**
* Saves data added/edited in metaboxes in Post and Page screen
*
* Hooked to 'save_post'
*
* Adapted from Write Panel plugin by Nathan Rice
*
*/
function sgr_save_metabox_data($post_id, $post) {
	
	// Check referrer is from DCG metabox
	if ( !wp_verify_nonce( $_POST['sgr_metabox_noncename'], SGR_FILE_HOOK )) {
	return $post->ID;
	}

	// Is the user allowed to edit the post or page?
	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post->ID ))
		return $post->ID;
	} else {
		if ( !current_user_can( 'edit_post', $post->ID ))
		return $post->ID;
	}

	// Build array from $_POST data	
	$newdata['_sgr-image'] = $_POST['_sgr-image'];
	$newdata['_sgr-desc'] = $_POST['_sgr-desc'];
	
	/* Sanitise data */
	
	// trim whitespace - all options
	foreach( $newdata as $key => $value ) {
		$input[$key] = trim($value);
	}
	
	// Deal with Image
	$newdata['_sgr-image'] = esc_url_raw( $newdata['_sgr-image'] );
	
	// Deal with Text
	$allowed_html = array( 'a' => array('href' => array(),'title' => array() ), 'br' => array(), 'em' => array(), 'strong' => array() );
	$allowed_protocols = array( 'http', 'https', 'mailto', 'feed' );
	
	$newdata['_sgr-desc'] = wp_kses( $newdata['_sgr-desc'], $allowed_html, $allowed_protocols );
	
	
	
	// Add values of $newdata as custom fields
	
	foreach ($newdata as $key => $value) {
		
		if( $post->post_type == 'revision' ) return; //don't store custom data twice
		
		$value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)
		
		if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value
			update_post_meta($post->ID, $key, $value);
		} else { //if the custom field doesn't have a value
			add_post_meta($post->ID, $key, $value);
		}
		
		if(!$value) delete_post_meta($post->ID, $key); //delete if any are blank, eg _dfcg-exclude is NULL
	}
}