<?php
/**
 * Adds Foo_Widget widget.
 * make sure to include in lib/widgets.php
 */
class CustomWP_Widget extends WP_Widget {

	protected $fields 			= array();

	/**
	 * Register widget with WordPress.
	 */
	function __construct($baseID, $name, $args, $fieldsArgs) {
		/*
		parent::__construct(
			'foo_widget', // Base ID
			__('Widget Title', 'text_domain'), // Name
			array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args
		);
		*/
		parent::__construct( $baseID, $name, $args );

		$this->fields = $fieldsArgs;

		/*
		$fieldsArgs = array(
			'fieldName' => array(
					'label'			=> '',
					'input'			=> '<input id="[%ID%]" name="[%NAME%]" class="[%CLASS%] custom-class" type="text" value="[%VALUE%]" />';
					'defaultValue'	=> '',
					'value'			=> 'internally used', 					
				)
			)
		*/
	}


	private function __str_replace($subject, $search_array) {
		foreach ( $search_array as $search_value => $replace_value ) {
			$subject = str_replace($search_value, $replace_value, $subject);
		}
		//var_dump($subject);

		return $subject;
	}


	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */

	public function form( $instance ) {		
		//var_dump($instance);
		
		foreach ($this->fields as $fieldName => $field) {

			// var_dump($field);			
			if ( isset( $instance[ $fieldName ] ) ) {
				$this->fields[$fieldName]['value'] = $instance[ $fieldName ];
			} else {
				$this->fields[$fieldName]['value'] = $this->fields[$fieldName]['defaultValue'];
			}

			?>
			<p>
				<label for="<?php echo $this->get_field_id($fieldName); ?>"><?php _e( $field['label'] ); ?></label>
				<?php
					$t = $this->__str_replace($field['input'], array(
								"[%ID%]" 					=> $this->get_field_id($fieldName),
								"[%NAME%]"				=> $this->get_field_name($fieldName),
								"[%CLASS%]"				=> '',
								"[%VALUE%]"				=> $this->fields[$fieldName]['value'],
								"[%PLACEHOLDER%]" => $this->fields[$fieldName]['defaultValue'],
						 ));

					if (array_key_exists('select-values', $field) ) {
						$opts = '';

						foreach ($field['select-values'] as $k=>$v) {
							$opts .= '<option value="'.$k.'"'.(($this->fields[$fieldName]['value'] == $k)?' selected':'').'>'.$v.'</option>';
						}
						$t = $this->__str_replace($t, array(
								"[%SELECT_VALUES%]" => $opts
								));						
					}
					echo $t;
				?>
			</p>
			<?php

			//var_dump($this->fields[$fieldName]);
		}
	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */

	public function update( $new_instance, $old_instance ) {
		$instance = array();

		foreach ($new_instance as $fieldName=>$fieldValue) {
			$instance[$fieldName] = ( ! empty( $fieldValue ) ) ? strip_tags( $fieldValue ) : '';
		}

		return $instance;
	}

} // class CustomWP_Widget

?>