<?php
/**
 * Ads 125x125 widget.
 *
 * @package    ShoBe
 * @author     Boby Hermez
 * @copyright  Copyright (c) 2025, Boby Hermez
 * @license    http://www.gnu.org/licenses/gpl-2.0.html
 * @since      1.0.0
 */
class shobe_Ads125_Widget extends WP_Widget {

	/**
	 * Sets up the widgets.
	 *
	 * @since 1.0.0
	 */
	function __construct() {

		// Set up the widget options.
		$shobe_widget_options = array(
			'classname'   => 'widget_shobe_ads_125',
			'description' => __( 'Easily to display any type of ad.', 'shobe' )
		);

		// Create the widget.
		parent::__construct(
			'shobe-ad125',                                  // $this->id_base
			__( '&raquo; ShoBe Advertisement 125x125', 'shobe' ), // $this->name
			$shobe_widget_options                                  // $this->shobe_widget_options
		);
	}

	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 *
	 * @since 1.0.0
	 */
	function widget( $args, $instance ) {
		extract( $args );

		// Output the theme's $before_widget wrapper.
		echo $before_widget;

		// If the title not empty, display it.
		if ( $instance['title'] ) {
			echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
		}

			// Display the ad banner.
			if ( $instance['ad_code1'] ) {
				echo stripslashes( $instance['ad_code1'] );
			}

			if ( $instance['ad_code2'] ) {
				echo '<span class="img-right">' . stripslashes( $instance['ad_code2'] ) . '</span>';
			}

			if ( $instance['ad_code3'] ) {
				echo stripslashes( $instance['ad_code3'] );
			}

			if ( $instance['ad_code4'] ) {
				echo '<span class="img-right">' . stripslashes( $instance['ad_code4'] ) . '</span>';
			}

		// Close the theme's widget wrapper.
		echo $after_widget;

	}

	/**
	 * Updates the widget control options for the particular instance of the widget.
	 *
	 * @since 1.0.0
	 */
	function update( $new_instance, $old_instance ) {

		$instance = $new_instance;

		$instance['title'] = ( ! empty( $instance['title'] ) ) ? sanitize_text_field( strip_tags( $instance['title'] ) ): '';
		$instance['ad_code1'] = ( ! empty( $instance['ad_code1'] ) ) ? tag_escape( $instance['ad_code1'] ) : '';
		$instance['ad_code2'] = ( ! empty( $instance['ad_code2'] ) ) ? tag_escape( $instance['ad_code2'] ) : '';
		$instance['ad_code3'] = ( ! empty( $instance['ad_code3'] ) ) ? tag_escape( $instance['ad_code3'] ) : '';
		$instance['ad_code4'] = ( ! empty( $instance['ad_code4'] ) ) ? tag_escape( $instance['ad_code4'] ) : '';

		return $instance;
	}

	/**
	 * Displays the widget control options in the Widgets admin screen.
	 *
	 * @since 1.0.0
	 */
	function form( $instance ) {

		// Default value.
		$defaults = array(
			'title'    => esc_html__( 'Advertisement', 'shobe' ),
			'ad_code1' => '',
			'ad_code2' => '',
			'ad_code3' => '',
			'ad_code4' => '',
		);

		$instance = wp_parse_args( (array) $instance, $defaults );
	?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>">
				<?php _e( 'Title:', 'shobe' ); ?>
			</label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'ad_code1' ); ?>">
				<?php _e( 'Ad Code 1:', 'shobe' ); ?>
			</label>
			<textarea class="widefat" name="<?php echo $this->get_field_name( 'ad_code1' ); ?>" id="<?php echo $this->get_field_id( 'ad_code1' ); ?>" cols="30" rows="6"><?php echo stripslashes( $instance['ad_code1'] ); ?></textarea>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'ad_code2' ); ?>">
				<?php _e( 'Ad Code 2:', 'shobe' ); ?>
			</label>
			<textarea class="widefat" name="<?php echo $this->get_field_name( 'ad_code2' ); ?>" id="<?php echo $this->get_field_id( 'ad_code2' ); ?>" cols="30" rows="6"><?php echo stripslashes( $instance['ad_code2'] ); ?></textarea>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'ad_code3' ); ?>">
				<?php _e( 'Ad Code 3:', 'shobe' ); ?>
			</label>
			<textarea class="widefat" name="<?php echo $this->get_field_name( 'ad_code3' ); ?>" id="<?php echo $this->get_field_id( 'ad_code3' ); ?>" cols="30" rows="6"><?php echo stripslashes( $instance['ad_code3'] ); ?></textarea>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'ad_code4' ); ?>">
				<?php _e( 'Ad Code 4:', 'shobe' ); ?>
			</label>
			<textarea class="widefat" name="<?php echo $this->get_field_name( 'ad_code4' ); ?>" id="<?php echo $this->get_field_id( 'ad_code4' ); ?>" cols="30" rows="6"><?php echo stripslashes( $instance['ad_code4'] ); ?></textarea>
		</p>
	<?php

	}

}

function register_shobe_ads125_Widget() {
	register_widget( 'shobe_Ads125_Widget' );
}
add_action( 'widgets_init', 'register_shobe_ads125_Widget' );