<?php
/**
 * Register our sidebars and widgetized areas. Also register the default Epherma widget.
 *
 * @since Catch Flames Pro 1.0
 */
function catchflames_widgets_init() {

	register_widget( 'catchflames_adwidget' );

	register_widget( 'catchflames_social_widget' );


	//Primary Sidebar
	register_sidebar( array(
		'name'			=> __( 'Primary Sidebar', 'catch-flames' ),
		'id'			=> 'sidebar-1',
		'description'	=> __( 'This is the primary sidebar if you are using a two or three column site layout option.', 'catch-flames' ),
		'before_widget'	=> '<aside id="%1$s" class="widget %2$s">',
		'after_widget'	=> "</aside>",
		'before_title'	=> '<h3 class="widget-title">',
		'after_title'	=> '</h3>',
	) );

	//Secondary Sidebar
	register_sidebar( array(
		'name'			=> __( 'Secondary Sidebar', 'catch-flames' ),
		'id'			=> 'catchflames_third',
		'description'	=> __( 'This is the secondary sidebar if you are using a three column site layout option.', 'catch-flames' ),
		'before_widget'	=> '<aside id="%1$s" class="widget %2$s">',
		'after_widget'	=> "</aside>",
		'before_title'	=> '<h3 class="widget-title">',
		'after_title'	=> '</h3>',
	) );

	//Footer One Sidebar
	register_sidebar( array(
		'name' => __( 'Footer Area One', 'catch-flames' ),
		'id' => 'sidebar-2',
		'description' => __( 'An optional widget area for your site footer', 'catch-flames' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	//Footer Two Sidebar
	register_sidebar( array(
		'name' => __( 'Footer Area Two', 'catch-flames' ),
		'id' => 'sidebar-3',
		'description' => __( 'An optional widget area for your site footer', 'catch-flames' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	//Footer Three Sidebar
	register_sidebar( array(
		'name' => __( 'Footer Area Three', 'catch-flames' ),
		'id' => 'sidebar-4',
		'description' => __( 'An optional widget area for your site footer', 'catch-flames' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	//Footer Four Sidebar
	register_sidebar( array(
		'name' => __( 'Footer Area Four', 'catch-flames' ),
		'id' => 'sidebar-5',
		'description' => __( 'An optional widget area for your site footer', 'catch-flames' ),
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Header Right Sidebar
	register_sidebar( array(
		'name'			=> __( 'Header Right Sidebar', 'catch-flames' ),
		'id'			=> 'catchflames_header_right_sidebar',
		'description'   => __( 'This is the header right widget area. It typically appears on the right of the site title or logo. This widget area is not equipped to display any widget, and works best with a custom menu, a search form, social icons widget or possibly a text widget.', 'catch-flames' ),
		'before_widget'	=> '<aside id="%1$s" class="widget %2$s">',
		'after_widget'	=> "</aside>",
		'before_title'	=> '<h3 class="widget-title">',
		'after_title'	=> '</h3>',
	) );

	// Header Top Sidebar
	register_sidebar( array(
		'name'			=> __( 'Header Top Sidebar', 'catch-flames' ),
		'id'			=> 'catchflames_header_top_sidebar',
		'description'   => __( 'This is the header top widget area. It typically appears on the top of the site title or logo. To show this, first you need to enable it from Appearance -> Theme Options => Fixed Header Top Options.This widget area is not equipped to display any widget, and works best with logo, fixed header top menu and default social & search icons or possibly a text widget.', 'catch-flames' ),
		'before_widget'	=> '<aside id="%1$s" class="widget %2$s">',
		'after_widget'	=> "</aside>",
		'before_title'	=> '<h3 class="widget-title">',
		'after_title'	=> '</h3>',
	) );

	// Optional Homepage Sidebar
	register_sidebar( array(
		'name' => __( 'Homepage Sidebar', 'catch-flames' ),
		'id' => 'catchflames_home_sidebar',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Optional Archive Sidebar
	register_sidebar( array(
		'name' => __( 'Archive Sidebar', 'catch-flames' ),
		'id' => 'catchflames_archive_sidebar',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Optional Page Sidebar
	register_sidebar( array(
		'name' => __( 'Page Sidebar', 'catch-flames' ),
		'id' => 'catchflames_page_sidebar',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Optional Post Sidebar
	register_sidebar( array(
		'name' => __( 'Post Sidebar', 'catch-flames' ),
		'id' => 'catchflames_post_sidebar',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Optional Sidebar One
	register_sidebar( array(
		'name' => __( 'Optional Sidebar One', 'catch-flames' ),
		'id' => 'catchflames_optional_sidebar_one',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Optional Sidebar Two
	register_sidebar( array(
		'name' => __( 'Optional Sidebar Two', 'catch-flames' ),
		'id' => 'catchflames_optional_sidebar_two',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Optional Sidebar Three
	register_sidebar( array(
		'name' => __( 'Optional Sidebar Three', 'catch-flames' ),
		'id' => 'catchflames_optional_sidebar_three',
		'before_widget' => '<aside id="%1$s" class="widget %2$s">',
		'after_widget' => "</aside>",
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// WooCommerce Sidebar
	if ( class_exists( 'Woocommerce' ) ) {
		register_sidebar( array(
			'name' => __( 'WooCommerce Sidebar', 'catch-flames' ),
			'id' => 'catchflames_woocommerce_sidebar',
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget' => "</aside>",
			'before_title' => '<h3 class="widget-title">',
			'after_title' => '</h3>',
		) );
	}
}
add_action( 'widgets_init', 'catchflames_widgets_init' );


/**
 * Makes a custom Widget for Displaying Ads
 *
 * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
 *
 * @package Catch Themes
 * @subpackage Catch Flames Pro
 * @since Catch Flames Pro 1.0
 */
class catchflames_adwidget extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'widget_catchflames_adwidget', // Base ID
			__( 'CT: Adspace Widget', 'catch-flames' ), // Name
			array( 'description' => __( 'Use this widget to add any type of Ad as a widget.', 'catch-flames' ) ) // Args
		);
	}

	/**
	 * Creates the form for the widget in the back-end which includes the Title , adcode, image, alt
	 * $instance Current settings
	 */
	function form($instance) {
		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'adcode' => '', 'image' => '', 'href' => '', 'target' => '0', 'alt' => '' ) );
		$title = esc_attr( $instance[ 'title' ] );
		$adcode = esc_textarea( $instance[ 'adcode' ] );
		$image = esc_url( $instance[ 'image' ] );
		$href = esc_url( $instance[ 'href' ] );
		$target = $instance['target'] ? 'checked="checked"' : '';
		$alt = esc_attr( $instance[ 'alt' ] );
		if ( isset( $instance['enable_hide_404'] ) ) {
			if ( $instance['enable_hide_404'] ) {
				$enable_hide_404 = 'checked="checked"';
			}
			else {
				$enable_hide_404 = '';
			}
		}
		else {
			$enable_hide_404 = 'checked="checked"';
		}
		?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title (optional):','catch-flames'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" />
        </p>
        <?php if ( current_user_can( 'unfiltered_html' ) ) : // Only show it to users who can edit it ?>
            <p>
                <label for="<?php echo $this->get_field_id('adcode'); ?>"><?php _e('Ad Code:','catch-flames'); ?></label>
                <textarea name="<?php echo $this->get_field_name('adcode'); ?>" class="widefat" id="<?php echo $this->get_field_id('adcode'); ?>"><?php echo $adcode; ?></textarea>
            </p>
            <p><strong>or</strong></p>
        <?php endif; ?>
        <p>
            <label for="<?php echo $this->get_field_id('image'); ?>"><?php _e('Image Url:','catch-flames'); ?></label>
        <input type="text" name="<?php echo $this->get_field_name('image'); ?>" value="<?php echo $image; ?>" class="widefat" id="<?php echo $this->get_field_id('image'); ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('href'); ?>"><?php _e('Link URL:','catch-flames'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name('href'); ?>" value="<?php echo esc_url( $href ); ?>" class="widefat" id="<?php echo $this->get_field_id('href'); ?>" />
        </p>
		<p>
        	<label for="<?php echo $this->get_field_id('target'); ?>"><?php _e( 'Open Link in New Window', 'catch-flames' ); ?></label>
			<input class="checkbox" type="checkbox" <?php echo $target; ?> id="<?php echo $this->get_field_id('target'); ?>" name="<?php echo $this->get_field_name('target'); ?>" />
		</p>
        <p>
            <label for="<?php echo $this->get_field_id('alt'); ?>"><?php _e('Alt text:','catch-flames'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name('alt'); ?>" value="<?php echo $alt; ?>" class="widefat" id="<?php echo $this->get_field_id('alt'); ?>" />
        </p>
        <p>
			<input class="checkbox" type="checkbox" <?php echo $enable_hide_404; ?> id="<?php echo $this->get_field_id('enable_hide_404'); ?>" name="<?php echo $this->get_field_name('enable_hide_404'); ?>" /> <label for="<?php echo $this->get_field_id('enable_hide_404'); ?>"><?php _e( 'Check to Hide Ad on 404 page', 'catch-flames' ); ?></label>
		</p>
        <?php
	}

	/**
	 * update the particular instant
	 *
	 * This function should check that $new_instance is set correctly.
	 * The newly calculated value of $instance should be returned.
	 * If "false" is returned, the instance won't be saved/updated.
	 *
	 * $new_instance New settings for this instance as input by the user via form()
	 * $old_instance Old settings for this instance
	 * Settings to save or bool false to cancel saving
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['adcode'] = wp_kses_stripslashes($new_instance['adcode']);
		$instance['image'] = esc_url_raw($new_instance['image']);
		$instance['href'] = esc_url_raw($new_instance['href']);
		$instance['target'] = isset( $new_instance['target'] ) ? 1 : 0;
		$instance['alt'] = sanitize_text_field($new_instance['alt']);

		$instance[ 'enable_hide_404' ] = isset( $new_instance[ 'enable_hide_404' ] ) ? 1 : 0;

		if ( function_exists( 'icl_register_string' ) ) {
      		icl_register_string('Alt text', 'alttext - ' . $this->id, $instance['alt']);
  		 }

		return $instance;
	}

	/**
	 * Displays the Widget in the front-end.
	 *
	 * $args Display arguments including before_title, after_title, before_widget, and after_widget.
	 * $instance The settings for the particular instance of the widget
	 */
	function widget( $args, $instance ) {
		extract( $args );
		extract( $instance );

		if ( isset( $instance['enable_hide_404'] ) && $instance['enable_hide_404'] && is_404() ) {
			//Bail Early if the page is 404 error page and the widget is set to be hidden in that page
			return;
		}

		$instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$instance['alt'] = apply_filters( 'widget_text', empty( $instance['alt'] ) ? '' : $instance['alt'], $instance );

		$icl_t = function_exists('icl_t');

		if ( !empty( $instance['alt'] ) ) {
			$alt = $icl_t ? icl_t( 'Alt Text', 'alttext - ' . $this->id, $instance['alt'] ) : $instance['alt'];
		}
		else {
			$alt = '';
		}

		$adcode = !empty( $instance['adcode'] ) ? $instance[ 'adcode' ] : '';
		$image = !empty( $instance['image'] ) ? $instance[ 'image' ] : '';
		$href = !empty( $instance['href'] ) ? $instance[ 'href' ] : '';
		$target = !empty( $instance['target'] ) ? 'true' : 'false';

		$enable_hide_404 = !empty( $instance[ 'enable_hide_404' ] ) ? 'true' : 'false';

		if ( $target == "true" ) {
			$base = '_blank';
		}
		else {
			$base = '_self';
		}

		echo $before_widget;

		if ( !empty( $instance['title'] ) ) {
			echo $before_title . $instance['title'] . $after_title;
		}
		else {
			echo '<span class="paddingtop"></span>';
		}

		if ( $adcode != '' ) {
			echo $adcode;
		}
		elseif ( $image != '' ) {
			echo '<a href="' . $href . '" target="' . $base . '"><img src="'. $image . '" alt="' . $alt . '" /></a>';
		}
		else {
			echo '<!-- No Image or Ads Code -->';
		}

		echo $after_widget;

	}

}


/**
 * Social Icons
 *
 * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
 *
 * @package Catch Themes
 * @subpackage Catch Flames Pro
 * @since Catch Flames Pro 1.0
 */
class catchflames_social_widget extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'widget_catchflames_social_widget', // Base ID
			__( 'CT: Social Widget', 'catch-flames' ), // Name
			array( 'description' => __( 'Use this widget to add Social Icons from Social Icons Settings as a widget.', 'catch-flames' ) ) // Args
		);
	}

	/**
	 * Creates the form for the widget in the back-end which includes the Title , adcode, image, alt
	 * $instance Current settings
	 */
	function form($instance) {
		$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
		$title = esc_attr( $instance[ 'title' ] );
		?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title (optional):','catch-flames'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" />
        </p>
        <?php
	}

	/**
	 * update the particular instant
	 *
	 * This function should check that $new_instance is set correctly.
	 * The newly calculated value of $instance should be returned.
	 * If "false" is returned, the instance won't be saved/updated.
	 *
	 * $new_instance New settings for this instance as input by the user via form()
	 * $old_instance Old settings for this instance
	 * Settings to save or bool false to cancel saving
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);

		return $instance;
	}

	/**
	 * Displays the Widget in the front-end.
	 *
	 * $args Display arguments including before_title, after_title, before_widget, and after_widget.
	 * $instance The settings for the particular instance of the widget
	 */
	function widget( $args, $instance ) {
		extract( $args );
		extract( $instance );
		$title = !empty( $instance['title'] ) ? $instance[ 'title' ] : '';

		echo $before_widget;
		if ( $title != '' ) {
			echo $before_title . apply_filters( 'widget_title', $title, $instance, $this->id_base ) . $after_title;
		}

		catchflames_social_networks();

		echo $after_widget;
	}

}