<?php
/**
 * @package hello world edit
 * @author Frank B&uuml;ltge
 * @version 0.1
 */
 
/*
    Plugin Name: hello world edit
    Plugin URI: http://www.wordpressbuch.de/
    Description: hello world edit - wir schreiben ein einfaches WP-Plugin f&uuml;r den Edit-Bereich.
    Author: Frank B&uuml;ltge
    Version: 0.1
    License: GPL
    Author URI: http://bueltge.de/
    Last change: 15.04.2009 14:52:11
*/

global $wp_version;
if ( !
function_exists ('add_action') || version_compare($wp_version"2.7dev""<") ) {
    if ( 
function_exists ('add_action') )
        
$exit_msg 'The plugin <em>Page Tags</em> requires WordPress 2.7 or newer. <a href="http://codex.wordpress.org/Upgrading_WordPress">Please update WordPress</a> or delete the plugin.';
    else
        
$exit_msg '';
    
header('Status: 403 Forbidden');
    
header('HTTP/1.1 403 Forbidden');
    exit(
$exit_msg);
}

if ( 
function_exists('add_action') ) {
    
//WordPress definitions
    
if ( !defined('WP_CONTENT_URL') )
        
define('WP_CONTENT_URL'get_option('siteurl') . '/wp-content');
    if ( !
defined('WP_CONTENT_DIR') )
        
define('WP_CONTENT_DIR'ABSPATH 'wp-content');
    if ( !
defined('WP_PLUGIN_URL') )
        
define('WP_PLUGIN_URL'WP_CONTENT_URL.'/plugins');
    if ( !
defined('WP_PLUGIN_DIR') )
        
define('WP_PLUGIN_DIR'WP_CONTENT_DIR.'/plugins');
    if ( !
defined('PLUGINDIR') )
        
define'PLUGINDIR''wp-content/plugins' ); // Relative to ABSPATH.  For back compat.
    
if ( !defined('WP_LANG_DIR') )
        
define('WP_LANG_DIR'WP_CONTENT_DIR '/languages');

    
// plugin definitions
    
define'FB_HWE_BASENAME'plugin_basename(__FILE__) );
    
define'FB_HWE_TEXTDOMAIN''mygermancityfacts' );
}

if ( !
class_exists'HelloWorldEdit' ) ) {
    class 
HelloWorldEdit {

        
// constructor
        
function HelloWorldEdit() {
            
            
add_action'init', array(&$this'textdomain') );
            
add_action'admin_init', array(&$this'on_admin_init') );
            
add_action'wp_insert_post', array(&$this'on_wp_insert_post'), 10);
        }
        
        
// load gettextfile
        
function textdomain() {
            
            if ( 
function_exists('load_plugin_textdomain') )
                
load_plugin_textdomainFB_HWE_TEXTDOMAINfalsedirnameFB_MGCF_BASENAME ) . '/languages');
        }
        
        
// admin init
        
function on_admin_init() {
            
            
add_meta_box'HelloWorldEdit',
                            
__('Hello World Edit',
                            
FB_HWE_TEXTDOMAIN),
                            array(&
$this'meta_box'),
                            
'post''side''core'
                        
);
            
add_meta_box'HelloWorldEdit',
                            
__('Hello World Edit',
                            
FB_HWE_TEXTDOMAIN),
                            array(&
$this'meta_box'),
                            
'page''side''core'
                        
);
        }
        
        
// check for preview
        
function is_page_preview() {
            
            
$id = (int)$_GET['preview_id'];
            if (
$id == 0$id = (int)$_GET['post_id'];
            
$preview $_GET['preview'];
            if (
$id && $preview == 'true') {
                global 
$wpdb;
                
$type $wpdb->get_results("SELECT post_type FROM $wpdb->posts WHERE ID=$id");
                if (
count($type) && ($type[0]->post_type == 'page') && current_user_can('edit_page')) return true;
            }
            return 
false;
        }
        
        
// after save post, save meta data for plugin
        
function on_wp_insert_post($id) {
            global 
$id;

            if ( !isset(
$id) )
                
$id = (int)$_REQUEST['post_ID'];
            if ( 
$this->is_page_preview() && !isset($id) )
                
$id = (int)$_GET['preview_id'];

            if ( !
current_user_can('edit_post') )
                return;

            if ( isset(
$_POST['hwe-value1']) )
                
$this->data['value1'] = $_POST['hwe-value1'];
            if ( isset(
$_POST['hwe-value2']) )
                
$this->data['value2'] = $_POST['hwe-value2'];

            
update_post_meta($id'HelloWorldEdit'$this->data);
        }
        
        
// meta box on post/page
        
function meta_box($data) {
            
            
$value get_post_meta($data->ID'HelloWorldEdit'true);
            
?>
            <table id="hwe-value-definition" width="100%" cellspacing="5px">
                <tr>
                    <td><label for="hwe-value1"><?php _e'Value 1:'FB_HWE_TEXTDOMAIN ); ?></label></td>
                    <td><input type="text" id="hwe-value1" name="hwe-value1" class="value1 form-input-tip" size="20" autocomplete="off" value="<?php echo $value['value1']; ?>" tabindex="6" /></td>
                </tr>
                <tr>
                    <td><label for="hwe-value2"><?php _e'Value 2:'FB_HWE_TEXTDOMAIN); ?></label></td>
                    <td><input type="text" id="hwe-value2" name="hwe-value2" class="value2 form-input-tip" size="20" autocomplete="off" value="<?php echo $value['value2']; ?>" tabindex="6" /></td>
                </tr>
            </table>
            <?php
        
}
        
        
// return values
        
function values($id) {
            
            
$id = (int) $id;
            
$return '';
            if ( 
is_singular() && $id ) {
                
$value get_post_meta($id'HelloWorldEdit'true);
                
                if (
$value['value1'])
                    
$return .= "\n\t" __'Value 1:'FB_HWE_TEXTDOMAIN ) . ' ' $value['value1'] . '<br />';
                if (
$value['value2'])
                    
$return .= "\n\t" __'Value 2:'FB_HWE_TEXTDOMAIN ) . ' ' $value['value2'] . '<br />';

                
                return 
$return;
            }
        }
        
    }
    
    
$HelloWorldEdit = new HelloWorldEdit();
    
    
    
// use in template
    
function the_values($id) {
        global 
$HelloWorldEdit;
        
        echo 
$HelloWorldEdit->values($id);
    }
}
?>