<?php
/**
 *  Header Image Implementation
 *
 * @package Education Soul
 */

/**
 * Sets up the WordPress core custom header and custom background features.
 *
 * @since 1.0
 *
 * @see education_soul_header_style()
 */
function education_soul_custom_header_and_bg() {
	/**
	 * Filter the arguments used when adding 'custom-header' support in Education Soul.
	 *
	 * @since 1.0
	 *
	 * @param array $args {
	 *     An array of custom-header support arguments.
	 *
	 *     @type string $default-text-color Default color of the header text.
	 *     @type int      $width            Width in pixels of the custom header image. Default 1200.
	 *     @type int      $height           Height in pixels of the custom header image. Default 280.
	 *     @type bool     $flex-height      Whether to allow flexible-height header images. Default true.
	 *     @type callable $wp-head-callback Callback function used to style the header image and text
	 *                                      displayed on the blog.
	 * }
	 */
	add_theme_support( 'custom-header', apply_filters( 'education_soul_custom_header_args', array(
		'default-image' => get_parent_theme_file_uri( '/images/slide1.jpg' ),
		'width'         => 1920,
		'height'        => 842,
		'flex-height'   => true,
		'flex-height'   => true,
	) ) );

	register_default_headers( array(
		'default-image' => array(
			'url'           => '%s/images/slide1.jpg',
			'thumbnail_url' => '%s/images/slide1-thumbnail.jpg',
			'description'   => esc_html__( 'Default Header Image', 'education-soul-pro' ),
		),
		'second-image' => array(
			'url'           => '%s/images/slide2.jpg',
			'thumbnail_url' => '%s/images/slide2-thumbnail.jpg',
			'description'   => esc_html__( 'Alternate Header Image', 'education-soul-pro' ),
		),
	) );
}
add_action( 'after_setup_theme', 'education_soul_custom_header_and_bg' );

if ( ! function_exists( 'education_soul_render_header_image' ) ) :
	/**
	 * Render header image.
	 */
	function education_soul_render_header_image () {
		$enable = education_soul_get_option( 'header_image_enable' );

		if ( !  ( ( 'homepage' === $enable && is_front_page() ) || 'entire-site' === $enable ) ) {
			// Bail if header image is disabled.
			return;
		}

		$target            = education_soul_get_option( 'header_image_url_target' ) ? '_blank' : '_self';
		$url               = education_soul_get_option( 'header_image_url' );
		$title             = education_soul_get_option( 'header_image_title' );
		$caption           = education_soul_get_option( 'header_image_caption' );
		$caption_alignment = education_soul_get_option( 'header_image_caption_alignment' );

		$image = get_header_image();

		$primary_button_text   = education_soul_get_option( 'header_image_primary_button_text' );
		$primary_button_url    = education_soul_get_option( 'header_image_primary_button_url' );
		$secondary_button_text = education_soul_get_option( 'header_image_secondary_button_text' );
		$secondary_button_url  = education_soul_get_option( 'header_image_secondary_button_url' );
		?>
		<div id="custom-header">
			<div class="wrapper">
					<?php
					// Buttons stuff.
					$buttons_markup = '';

					if ( $primary_button_text || $secondary_button_text ) {
						$buttons_markup .= '<div class="header-image-buttons">';
						if ( $primary_button_text ) {
							$buttons_markup .= '<a target="' . $target . '" href="' . esc_url( $primary_button_url ) . '" class="custom-button header-image-button button-primary">' . esc_html( $primary_button_text ) . '</a>';
						}
						if ( $secondary_button_text ) {
							$buttons_markup .= '<a target="' . $target . '" href="' . esc_url( $secondary_button_url ) . '" class="custom-button header-image-button button-secondary">' . esc_html( $secondary_button_text ) . '</a>';
						}
						$buttons_markup .= '</div>';
					}
					?>
					<article>

						<?php if ( $url ) : ?>
							<a href="<?php echo esc_url( $url ); ?>" target="<?php echo esc_attr( $target ); ?>" >
						<?php endif; ?>

							<img src="<?php echo esc_url( $image ); ?>" alt="<?php echo esc_attr( $title ); ?>" />

						<?php if ( $url ) : ?>
							</a>
						<?php endif; ?>

						<?php if ( $title || $caption || $buttons_markup ) : ?>
							<div class="header-image-caption caption-alignment-<?php echo esc_attr( $caption_alignment ); ?>">
								<div class="caption-wrap">
									<?php if ( $title ) : ?>
									<h3>
										<?php if ( $url ) : ?>
											<a href="<?php echo esc_url( $url ); ?>" target="<?php echo esc_attr( $target ); ?>" >
										<?php endif; ?>

										<?php echo esc_html( $title ); ?>

										<?php if ( $url ) : ?>
											</a>
										<?php endif; ?>
									</h3>
									<?php endif; ?>

									<?php if ( $caption ) : ?>
									<p><?php echo wp_kses_post( $caption ); ?></p>
									<?php endif; ?>

									<?php if ( $buttons_markup ) : ?>
									<?php echo wp_kses_post( $buttons_markup ); ?>
									<?php endif; ?>
								</div><!-- .caption-wrap -->
							</div><!-- .header-image-caption -->
						<?php endif; ?>
					</article>
			</div><!-- .wrapper -->
		</div><!-- #custom-header -->

		<?php
	}
	add_action( 'education_soul_action_before_content', 'education_soul_render_header_image', 6 );
endif;
