
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
/**
 * Widget MailChimp
 *
 * @link       https://codesupply.co
 * @since      1.0.0
 *
 * @package    Basic MailChimp
 * @subpackage Widget
 */

/**
 * Basic Widget MailChimp Class
 */
class Basic_MailChimp_Widget extends WP_Widget {

	/**
	 * Sets up a new widget instance.
	 */
	public function __construct() {

		$this->default_settings = array(
			'title'                 => esc_html__( 'MailChimp', 'basic-mailchimp' ),
			'text'                  => '',
			'list_id'               => null,
		);

		$widget_details = array(
			'classname' => 'basic_mailchimp_widget',
			'description' => esc_html__( 'A simple MailChimp subscribe form.', 'basic-mailchimp' ),
		);
		parent::__construct( 'basic_mailchimp_widget', esc_html__( 'MailChimp', 'basic-mailchimp' ), $widget_details );
	}

	/**
	 * Outputs the content for the current widget instance.
	 *
	 * @param array $args     Display arguments including 'before_title', 'after_title',
	 *                        'before_widget', and 'after_widget'.
	 * @param array $instance Settings for the current widget instance.
	 */
	public function widget( $args, $instance ) {

		$params = array_merge( $this->default_settings, $instance );

		// Before Widget.
		echo $args['before_widget']; ?>

		<div class="widget-body">

			<div class="bmc-wrap">

				<?php
				if ( $params['title'] ) {
					echo $args['before_title'] . apply_filters( 'widget_title', $params['title'], $instance, $this->id_base ) . $args['after_title'];
				} ?>

				<?php if ( $params['text'] ) { ?>
					<p class="bmc-message"><?php echo esc_html( $params['text'] ); ?></p>
				<?php } ?>

				<?php do_action( 'bmc_mailchimp_template', $params['list_id'] ); ?>

			</div>

		</div>

		<?php

		// After Widget.
		echo $args['after_widget'];
	}

	/**
	 * Handles updating settings for the current widget instance.
	 *
	 * @param array $new_instance New settings for this instance as input by the user via
	 *                            WP_Widget::form().
	 * @param array $old_instance Old settings for this instance.
	 * @return array Settings to save or bool false to cancel saving.
	 */
	public function update( $new_instance, $old_instance ) {
		$instance = $new_instance;

		return $instance;
	}

	/**
	 * Outputs the widget settings form.
	 *
	 * @param array $instance Current settings.
	 */
	public function form( $instance ) {
		$params = array_merge( $this->default_settings, $instance );
		?>
			<!-- Title -->
			<p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'basic-mailchimp' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $params['title'] ); ?>" /></p>

			<!-- Subscribe Message -->
			<p><label for="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>"><?php esc_html_e( 'Subscribe Message', 'basic-mailchimp' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'text' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'text' ) ); ?>" type="text" value="<?php echo esc_attr( $params['text'] ); ?>" /></p>


			<?php
			$token = get_option( 'bmc_mailchimp_token' );

			if ( $token ) {

				$mail_chimp = new MailChimp( $token );

				$data = $mail_chimp->get( 'lists' );

				if ( $data && isset( $data['lists'] ) && $data['lists'] ) {
				?>
					<!-- List -->
					<p><label for="<?php echo esc_attr( $this->get_field_id( 'list_id' ) ); ?>"><?php esc_html_e( 'List', 'basic-mailchimp' ); ?></label>
						<select class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'list_id' ) ); ?>" id="<?php echo esc_attr( $this->get_field_name( 'list_id' ) ); ?>">
							<option value="default"><?php esc_html_e( 'Default', 'basic-mailchimp' ); ?></option>
							<?php foreach ( $data['lists'] as $item ) : ?>
								<option <?php selected( $item['id'], $params['list_id'] ); ?> value="<?php echo esc_attr( $item['id'] ); ?>"><?php echo esc_html( $item['name'] ); ?></option>
							<?php endforeach; ?>
						</select>
					</p>
				<?php
				}
			}
			?>
		<?php
	}
}
