<?php
/*
Plugin Name: Display All Custom Fields Widget
Plugin URI: http://wptips.ru/display-all-custom-fields-for-a-post-widget-wordpress-plugin/
Description: Displays all custom fields for a chosen post id with key and value pairs formatted as an HTML definition list. Code based on PHP Code Widget by Otto.
Author: Alex
Version: 1.0
Text Domain: allfields-widget
Author URI: http://podojdi.ru
*/

class AllFieldsWidget extends WP_Widget {
	function AllFieldsWidget() {
		load_plugin_textdomain( 'allfields-widget', false, dirname( plugin_basename( __FILE__ ) ) );
		$widget_ops = array('classname' => 'widget_allfields', 'description' => __('Post ID', 'allfields-widget'));
		$control_ops = array('width' => 400, 'height' => 350);
		$this->WP_Widget('allfields', __('Display custom fields of a post', 'allfields-widget'), $widget_ops, $control_ops);
	}

	function widget( $args, $instance ) {
		extract($args);
		$title = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance );
		$text = apply_filters( 'widget_allfields', $instance['text'], $instance );
		echo $before_widget;
		if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
		$custom_fields = get_post_custom($text);
		if(!empty($custom_fields)) {echo '<dl class="metainfo">';}
		foreach ( $custom_fields as $field_key => $field_values ) {
			if(!isset($field_values[0])) continue;
			if(in_array($field_key,array('_edit_lock','_edit_last'))) continue; //exclude these meta keys
			echo '<dt>'.$field_key . '</dt><dd>' . $field_values[0].'</dd>';
		}
		if(!empty($custom_fields)) {echo '</dl>';}
			?>
			<div class="allfields-widget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
		<?php
		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['text'] =  $new_instance['text'];
		$instance['filter'] = isset($new_instance['filter']);
		return $instance;
	}

	function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
		$title = strip_tags($instance['title']);
		$text = format_to_edit($instance['text']);
?>
		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'allfields-widget'); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>

		<p><label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Post ID (may be a page/custom post):', 'allfields-widget'); ?></label><input class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" type="number" value="<?php echo $text; ?>"></p>
<?php
	}
}

add_action('widgets_init', create_function('', 'return register_widget("AllFieldsWidget");'));