<?php

/**
 * Footer About widget.
 *
 * @package shopbar
 */

defined( 'ABSPATH' ) || exit;


if( ! class_exists('Shopbar_Footer_About_Widget')) :

class Shopbar_Footer_About_Widget extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'shopbar_footer_about_widget', // Base ID
			esc_html__( 'Shopbar: Footer About Widget', 'shopbar' ), // Name
			array(
				'description' => esc_html__( 'Adds logo, description and social icons.', 'shopbar' ),
			)
		);
	}

	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget( $args, $instance ) {

		$subheading        = ! empty( $instance['subheading'] ) ? $instance['subheading'] : '';
		$logo_id           = ! empty( $instance['logo_id'] ) ? absint( $instance['logo_id'] ) : 0;
		$logo_width        = ! empty( $instance['logo_width'] ) ? absint( $instance['logo_width'] ) : 0;
		$cb_show_social    = ! empty( $instance['cb_show_social'] );

		$facebook_url   = ! empty( $instance['facebook_url'] ) ? $instance['facebook_url'] : '';
		$twitter_url    = ! empty( $instance['twitter_url'] ) ? $instance['twitter_url'] : '';
		$instagram_url  = ! empty( $instance['instagram_url'] ) ? $instance['instagram_url'] : '';
		$pinterest_url  = ! empty( $instance['pinterest_url'] ) ? $instance['pinterest_url'] : '';
		$youtube_url    = ! empty( $instance['youtube_url'] ) ? $instance['youtube_url'] : '';

		$has_social = ( $facebook_url || $twitter_url || $instagram_url || $pinterest_url || $youtube_url );

		?>

		<?php

		$logo_html = '';

		if ( $logo_id ) {
			$logo_attrs = array( 'class' => 'footer-logo-img' );
			if ( $logo_width ) {
				$logo_attrs['style'] = 'max-width:' . $logo_width . 'px; max-height:none; height:auto;';
			}
			$logo_html = wp_get_attachment_image( $logo_id, 'full', false, $logo_attrs );
		} elseif ( has_custom_logo() ) {
			$custom_logo_id = get_theme_mod( 'custom_logo' );
			$custom_logo_img = $custom_logo_id ? wp_get_attachment_image_url( $custom_logo_id, 'full' ) : '';
			if ( $custom_logo_img ) {
				$logo_html = '<img src="' . esc_url( $custom_logo_img ) . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" class="footer-logo-img">';
			}
		}
		?>

		<a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="footer-logo">
			<?php if ( $logo_html ) : ?>
				<?php echo wp_kses_post( $logo_html ); ?>
			<?php else : ?>
				<span class="footer-logo-icon"><?php shopbar_icon( 'bag', 22 ); ?></span>
				<span class="footer-logo-name"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></span>
			<?php endif; ?>
		</a>

		<?php
		// Description
		if ( $subheading ) {
			$desc_text = $subheading;
		} else {
			$theme_desc = get_theme_mod( 'shopbar_footer_about_text' );
			$desc_text  = ! empty( $theme_desc ) ? $theme_desc : __( 'Your one-stop shop for top quality products at the best prices. Smart choices, fast delivery, happy you.', 'shopbar' );
		}
		if ( $desc_text ) :
		?>
			<p class="footer-about-text"><?php echo esc_html( $desc_text ); ?></p>
		<?php endif; ?>

		<?php if ( $cb_show_social && $has_social ) : ?>
			<div class="footer-social">
				<?php if ( $facebook_url ) : ?>
					<a href="<?php echo esc_url( $facebook_url ); ?>" class="sb-social-icon" target="_blank" rel="noopener noreferrer" aria-label="<?php esc_attr_e( 'Facebook', 'shopbar' ); ?>"><?php shopbar_icon( 'facebook', 16 ); ?></a>
				<?php endif; ?>

				<?php if ( $instagram_url ) : ?>
					<a href="<?php echo esc_url( $instagram_url ); ?>" class="sb-social-icon" target="_blank" rel="noopener noreferrer" aria-label="<?php esc_attr_e( 'Instagram', 'shopbar' ); ?>"><?php shopbar_icon( 'instagram', 16 ); ?></a>
				<?php endif; ?>

				<?php if ( $twitter_url ) : ?>
					<a href="<?php echo esc_url( $twitter_url ); ?>" class="sb-social-icon" target="_blank" rel="noopener noreferrer" aria-label="<?php esc_attr_e( 'X (Twitter)', 'shopbar' ); ?>"><?php shopbar_icon( 'twitter', 16 ); ?></a>
				<?php endif; ?>

				<?php if ( $youtube_url ) : ?>
					<a href="<?php echo esc_url( $youtube_url ); ?>" class="sb-social-icon" target="_blank" rel="noopener noreferrer" aria-label="<?php esc_attr_e( 'YouTube', 'shopbar' ); ?>"><?php shopbar_icon( 'youtube', 16 ); ?></a>
				<?php endif; ?>

				<?php if ( $pinterest_url ) : ?>
					<a href="<?php echo esc_url( $pinterest_url ); ?>" class="sb-social-icon" target="_blank" rel="noopener noreferrer" aria-label="<?php esc_attr_e( 'Pinterest', 'shopbar' ); ?>"><?php shopbar_icon( 'pinterest', 16 ); ?></a>
				<?php endif; ?>
			</div>
		<?php endif; ?>

		<?php
	}

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

		$subheading        = isset( $instance['subheading'] ) ? $instance['subheading'] : '';
		$logo_id           = isset( $instance['logo_id'] ) ? $instance['logo_id'] : '';
		$logo_width        = isset( $instance['logo_width'] ) ? $instance['logo_width'] : '';
		$facebook_url      = isset( $instance['facebook_url'] ) ? $instance['facebook_url'] : '';
		$twitter_url       = isset( $instance['twitter_url'] ) ? $instance['twitter_url'] : '';
		$instagram_url     = isset( $instance['instagram_url'] ) ? $instance['instagram_url'] : '';
		$pinterest_url     = isset( $instance['pinterest_url'] ) ? $instance['pinterest_url'] : '';
		$youtube_url       = isset( $instance['youtube_url'] ) ? $instance['youtube_url'] : '';
		$cb_show_social    = isset( $instance['cb_show_social'] ) ? (bool) $instance['cb_show_social'] : true;

		$logo_url = $logo_id ? wp_get_attachment_url( absint( $logo_id ) ) : '';

		?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'subheading' ) ); ?>"><?php esc_html_e( 'Description:', 'shopbar' ); ?></label>
			<textarea class="widefat" rows="3" id="<?php echo esc_attr( $this->get_field_id( 'subheading' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'subheading' ) ); ?>"><?php echo esc_textarea( $subheading ); ?></textarea>
		</p>

		<hr style="margin:15px 0;border-top:1px solid #ddd">

		<p>
			<strong><?php esc_html_e( 'Footer Logo', 'shopbar' ); ?></strong><br>
			<span class="description" style="font-size:12px;color:#999;display:block;margin:4px 0 8px;"><?php esc_html_e( 'Upload a custom footer logo. No logo is shown until one is uploaded here.', 'shopbar' ); ?></span>
			<span class="shopbar-logo-preview" style="<?php echo $logo_url ? '' : 'display:none;'; ?>margin-bottom:8px;">
				<?php if ( $logo_url ) : ?>
					<img src="<?php echo esc_url( $logo_url ); ?>" style="max-width:100%;height:auto;border:1px solid #ddd;border-radius:4px;padding:4px;">
				<?php endif; ?>
			</span>
			<input type="hidden" class="shopbar-logo-id" id="<?php echo esc_attr( $this->get_field_id( 'logo_id' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'logo_id' ) ); ?>" value="<?php echo esc_attr( $logo_id ); ?>">
			<button type="button" class="button shopbar-logo-upload" data-frame-title="<?php esc_attr_e( 'Select Footer Logo', 'shopbar' ); ?>" data-frame-btn="<?php esc_attr_e( 'Use as Logo', 'shopbar' ); ?>"><?php esc_html_e( 'Upload Logo', 'shopbar' ); ?></button>
			<button type="button" class="button shopbar-logo-remove" style="<?php echo $logo_url ? '' : 'display:none;'; ?>"><?php esc_html_e( 'Remove', 'shopbar' ); ?></button>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'logo_width' ) ); ?>"><?php esc_html_e( 'Logo Width (px):', 'shopbar' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'logo_width' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'logo_width' ) ); ?>" type="number" min="40" max="600" step="1" value="<?php echo esc_attr( $logo_width ); ?>" placeholder="<?php esc_attr_e( 'auto', 'shopbar' ); ?>">
			<span class="description" style="font-size:12px;color:#999;display:block;margin:4px 0 0;"><?php esc_html_e( 'Set the maximum width of the footer logo. Leave empty for default size. The logo keeps its aspect ratio.', 'shopbar' ); ?></span>
		</p>

		<hr style="margin:15px 0;border-top:1px solid #ddd">

		<p><strong><?php esc_html_e( 'Social Media Icons', 'shopbar' ); ?></strong></p>

		<p>
			<input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'cb_show_social' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'cb_show_social' ) ); ?>" value="1" <?php checked( $cb_show_social ); ?>>
			<label for="<?php echo esc_attr( $this->get_field_id( 'cb_show_social' ) ); ?>"><?php esc_html_e( 'Show Social Icons', 'shopbar' ); ?></label>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'facebook_url' ) ); ?>"><?php esc_html_e( 'Facebook URL:', 'shopbar' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'facebook_url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'facebook_url' ) ); ?>" type="url" value="<?php echo esc_attr( $facebook_url ); ?>" placeholder="https://facebook.com/...">
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'twitter_url' ) ); ?>"><?php esc_html_e( 'X (Twitter) URL:', 'shopbar' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'twitter_url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'twitter_url' ) ); ?>" type="url" value="<?php echo esc_attr( $twitter_url ); ?>" placeholder="https://x.com/...">
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'instagram_url' ) ); ?>"><?php esc_html_e( 'Instagram URL:', 'shopbar' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'instagram_url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'instagram_url' ) ); ?>" type="url" value="<?php echo esc_attr( $instagram_url ); ?>" placeholder="https://instagram.com/...">
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'pinterest_url' ) ); ?>"><?php esc_html_e( 'Pinterest URL:', 'shopbar' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'pinterest_url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'pinterest_url' ) ); ?>" type="url" value="<?php echo esc_attr( $pinterest_url ); ?>" placeholder="https://pinterest.com/...">
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'youtube_url' ) ); ?>"><?php esc_html_e( 'YouTube URL:', 'shopbar' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'youtube_url' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'youtube_url' ) ); ?>" type="url" value="<?php echo esc_attr( $youtube_url ); ?>" placeholder="https://youtube.com/...">
		</p>

		<?php
	}

	/**
	 * 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();

		$instance['subheading']        = sanitize_textarea_field( $new_instance['subheading'] );
		$instance['logo_id']           = ! empty( $new_instance['logo_id'] ) ? absint( $new_instance['logo_id'] ) : '';
		$instance['logo_width']        = ! empty( $new_instance['logo_width'] ) ? absint( $new_instance['logo_width'] ) : '';
		$instance['facebook_url']      = isset( $new_instance['facebook_url'] ) ? esc_url_raw( $new_instance['facebook_url'] ) : '';
		$instance['twitter_url']       = isset( $new_instance['twitter_url'] ) ? esc_url_raw( $new_instance['twitter_url'] ) : '';
		$instance['instagram_url']     = isset( $new_instance['instagram_url'] ) ? esc_url_raw( $new_instance['instagram_url'] ) : '';
		$instance['pinterest_url']     = isset( $new_instance['pinterest_url'] ) ? esc_url_raw( $new_instance['pinterest_url'] ) : '';
		$instance['youtube_url']       = isset( $new_instance['youtube_url'] ) ? esc_url_raw( $new_instance['youtube_url'] ) : '';
		$instance['cb_show_social']    = ! empty( $new_instance['cb_show_social'] ) ? true : false;

		return $instance;
	}

}
endif;

if( ! function_exists('shopbar_register_footer_about_widget')) :
/**
 * Register widget.
 */
function shopbar_register_footer_about_widget() {
	register_widget( 'Shopbar_Footer_About_Widget' );
}
endif;

add_action( 'widgets_init', 'shopbar_register_footer_about_widget' );


/**
 * Enqueue media uploader scripts for the widget admin screen.
 */
if ( ! function_exists( 'shopbar_footer_about_widget_admin_scripts' ) ) :
function shopbar_footer_about_widget_admin_scripts( $hook ) {
	if ( 'widgets.php' !== $hook ) {
		return;
	}
	wp_enqueue_media();

	// The shared widget admin script is already enqueued by the announcement
	// bar widget. If this is the only widget loaded, enqueue it here too.
	if ( ! wp_script_is( 'shopbar-widget-admin', 'enqueued' ) ) {
		wp_enqueue_script(
			'shopbar-widget-admin',
			get_template_directory_uri() . '/assets/js/admin/widgets.js',
			array( 'jquery' ),
			wp_get_theme()->get( 'Version' ),
			true
		);


	}
}
endif;
add_action( 'admin_enqueue_scripts', 'shopbar_footer_about_widget_admin_scripts' );
