
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
/**
 * Widget Twitter
 *
 * @link       https://codesupply.co
 * @since      1.0.0
 *
 * @package    Basic Twitter
 * @subpackage Widget
 */

/**
 * Basic Widget Twitter Class
 */
class Basic_Twitter_Widget extends WP_Widget {

	/**
	 * Sets up a new widget instance.
	 */
	public function __construct() {

		$cache_timeout = (int) apply_filters( 'btw_cache_timeout', 60 );

		$this->default_settings = array(
			'title'                 => esc_html__( 'Twitter', 'basic-twitter' ),
			'username'              => '',
			'number'                => 5,
			'template'              => 'default',
			'header'                => true,
			'button'                => true,
			'retweets'              => false,
			'replies'               => false,
			'cache_time'            => $cache_timeout,
			'consumer_key'          => get_option( 'csco_twitter_consumer_key' ),
			'consumer_secret'       => get_option( 'csco_twitter_consumer_secret' ),
			'access_token'          => get_option( 'csco_twitter_access_token' ),
			'access_token_secret'   => get_option( 'csco_twitter_access_token_secret' ),
		);

		$widget_details = array(
			'classname' => 'basic_twitter_widget',
			'description' => esc_html__( 'A list of recent tweets.', 'basic-twitter' ),
		);
		parent::__construct( 'basic_twitter_widget', esc_html__( 'Twitter', 'basic-twitter' ), $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 );

		$id = md5( wp_json_encode( $params ) );

		// Before Widget.
		echo $args['before_widget'];
		?>

		<div class="widget-body">
			<?php

			// Title.
			if ( $params['title'] ) {
				echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $params['title'], $instance, $this->id_base ) . $args['after_title'] );
			}

			// Check if all fields have been filled in.
			if ( empty( $params['consumer_key'] ) || empty( $params['consumer_key'] ) || empty( $params['access_token'] ) || empty( $params['access_token_secret'] ) ) :
				?>
					<div class="alert alert-warning">
						<?php
							/* translators: Twitter Settings Link. */
							echo wp_kses_post( sprintf( __( 'Unable to load twitter. Please fill in all required <a href="%s" target="_blank">Twitter Settings</a>.', 'basic-twitter' ), esc_url( admin_url( 'options-general.php?page=btw_settings' ) ) ) );
						?>
					</div>
				<?php
			else :

				// Check if transient already exists.
				$twitter = get_transient( 'btw_widget_' . $id );

				if ( ! empty( $twitter ) ) {

					// Fetch twitter from the transient.
					$twitter = maybe_unserialize( $twitter );

				} else {

					// Get Twitter via Twitter OAuth.
					$twitter = btw_get_twitter(
						$params['consumer_key'],
						$params['consumer_secret'],
						$params['access_token'],
						$params['access_token_secret'],
						array(
							'screen_name'		=> $params['username'],
							'count'				=> $params['number'],
							'include_rts'		=> $params['retweets'] ? 'true' : 'false',
							'exclude_replies'	=> $params['replies'] ? 'false' : 'true',
						)
					);

					// Set a new transient if no errors returned.
					set_transient( 'btw_widget_' . $id, maybe_serialize( $twitter ), $params['cache_time'] * 60 );
				}

				// Check if errors have been returned.
				if ( ! empty( $twitter ) && isset( $twitter->errors ) ) {
					?>
						<div class="alert alert-warning">
							<?php echo wp_kses_post( $twitter->errors[0]->message ); ?>
						</div>
					<?php
				} elseif ( ! empty( $twitter ) && ! isset( $twitter->errors ) ) {

					// Check if there're valid twitter.
					if ( isset( $twitter ) && ! empty( $twitter ) && ! isset( $twitter->errors ) ) {
						btw_template_handler( $params['template'], $twitter, $params );
					}
				}
			endif;
			?>
		</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;

		// Display header.
		if ( ! isset( $instance['header'] ) ) {
			$instance['header'] = false;
		}

		// Display follow button.
		if ( ! isset( $instance['button'] ) ) {
			$instance['button'] = false;
		}

		// Include retweets.
		if ( ! isset( $instance['retweets'] ) ) {
			$instance['retweets'] = false;
		}

		// Include replies.
		if ( ! isset( $instance['replies'] ) ) {
			$instance['replies'] = false;
		}

		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-twitter' ); ?></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>

			<!-- Username -->
			<p><label for="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>"><?php esc_html_e( 'Username', 'basic-twitter' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'username' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'username' ) ); ?>" type="text" value="<?php echo esc_attr( $params['username'] ); ?>" /></p>

			<!-- Number of Tweets -->
			<p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of Tweets', 'basic-twitter' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="number" value="<?php echo esc_attr( $params['number'] ); ?>" /></p>

			<!-- Template -->
			<?php if ( btw_templates_check() ) : ?>
				<p><label for="<?php echo esc_attr( $this->get_field_id( 'Template' ) ); ?>"><?php esc_html_e( 'Template', 'basic-twitter' ); ?></label>
					<select name="<?php echo esc_attr( $this->get_field_name( 'template' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'template' ) ); ?>" class="widefat">
						<?php
						$templates = apply_filters( 'btw_templates', array() );
						if ( $templates ) {
							foreach ( $templates as $key => $item ) {
								?>
									<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $params['template'], $key ); ?>><?php echo esc_attr( $item['name'] ); ?></option>
								<?php
							}
						}
						?>
					</select>
				</p>
			<?php endif; ?>

			<!-- Display header -->
			<p><input id="<?php echo esc_attr( $this->get_field_id( 'header' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'header' ) ); ?>" type="checkbox" <?php checked( (bool) $params['header'] ); ?> />
			<label for="<?php echo esc_attr( $this->get_field_id( 'header' ) ); ?>"><?php esc_html_e( 'Display header', 'basic-twitter' ); ?></label></p>

			<!-- Display follow button -->
			<p><input id="<?php echo esc_attr( $this->get_field_id( 'button' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'button' ) ); ?>" type="checkbox" <?php checked( (bool) $params['button'] ); ?> />
			<label for="<?php echo esc_attr( $this->get_field_id( 'button' ) ); ?>"><?php esc_html_e( 'Display follow button', 'basic-twitter' ); ?></label></p>

			<!-- Display header -->
			<p><input id="<?php echo esc_attr( $this->get_field_id( 'retweets' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'retweets' ) ); ?>" type="checkbox" <?php checked( (bool) $params['retweets'] ); ?> />
			<label for="<?php echo esc_attr( $this->get_field_id( 'retweets' ) ); ?>"><?php esc_html_e( 'Include retweets', 'basic-twitter' ); ?></label></p>

			<!-- Display follow button -->
			<p><input id="<?php echo esc_attr( $this->get_field_id( 'replies' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'replies' ) ); ?>" type="checkbox" <?php checked( (bool) $params['replies'] ); ?> />
			<label for="<?php echo esc_attr( $this->get_field_id( 'replies' ) ); ?>"><?php esc_html_e( 'Include replies', 'basic-twitter' ); ?></label></p>

			<p class="alert alert-warning">
				<?php
					echo sprintf( '<a href="%2$s" target="_blank">%1$s</a>', esc_html__( 'Twitter Settings', 'basic-twitter' ), esc_url( admin_url( 'options-general.php?page=csco_settings&tab=twitter' ) ) );
				?>
			</p>
		<?php
	}
}
