<?php
/**
 * Creates the template for the author social icons block.
 *
 * PHP version 7.3
 *
 * @link       https://crosswindsframework.com/downloads/crosswinds-blocks
 * @since      1.0.0
 *
 * @package    Crosswinds_Blocks
 * @subpackage Crosswinds_Blocks/blocks/author-social-icons
 */

// If this file is called directly, then about execution.
if ( ! defined( 'WPINC' ) ) {
	die;
}

// Create the icons array.
$icons = array(
	'facebook' => array(
		'icon' => 'fab fa-facebook-f',
		'slug' => 'facebook',
		'name' => 'Facebook',
	),
	'twitter' => array(
		'icon' => 'fab fa-twitter',
		'slug' => 'twitter',
		'name' => 'Twitter',
	),
	'mastodon' => array(
		'icon' => 'fab fa-mastodon',
		'slug' => 'mastodon',
		'name' => 'Mastodon',
	),
	'instagram' => array(
		'icon' => 'fab fa-instagram',
		'slug' => 'instagram',
		'name' => 'Instagram',
	),
	'tumblr' => array(
		'icon' => 'fab fa-tumblr',
		'slug' => 'tumblr',
		'name' => 'Tumblr',
	),
	'email' => array(
		'icon' => 'far fa-envelope',
		'slug' => 'email',
		'name' => esc_html__( 'Email', 'crosswinds-blocks' ),
	),
	'website' => array(
		'icon' => 'far fa-link',
		'slug' => 'website',
		'name' => esc_html__( 'Website', 'crosswinds-blocks' ),
	),
);

// Add the styles for the icons.
if ( $attributes['iconColorValue'] ) {
	$icon_color = 'color: ' . $attributes['iconColorValue'] . ' !important;';
} else {
	$icon_color = '';
}

if ( $attributes['iconHoverColorValue'] ) {
	$icon_hover_color = 'color: ' . $attributes['iconHoverColorValue'] . ' !important;';
} else {
	$icon_hover_color = '';
}

if ( $attributes['iconBackgroundColorValue'] ) {
	$icon_background_color = 'background-color: ' . $attributes['iconBackgroundColorValue'] . ' !important;';
} else {
	$icon_background_color = '';
}

if ( $attributes['iconHoverBackgroundColorValue'] ) {
	$icon_hover_background_color = 'background-color: ' . $attributes['iconHoverBackgroundColorValue'] . ' !important;';
} else {
	$icon_hover_background_color = '';
}

if ( $attributes['iconsBorderRadius'] ) {
	$border_radius = 'border-radius: ' . $attributes['iconsBorderRadius'] . 'px;';
} else {
	$border_radius = '';
}

// Add in the classes for the block.
$wrapper_attributes = get_block_wrapper_attributes(
	array(
		'class' => $attributes['iconsDirection'] . '-icons ' . $attributes['iconsStretch'] . '-icons',
	)
);

?>
<?php var_dump( $attributes['socialIcons'] ); ?>

<div <?php echo wp_kses_post( $wrapper_attributes ); ?>>
	<style>
		.wp-block-crosswinds-blocks-author-social-icons .social-icon a {
			<?php echo esc_attr( $icon_background_color ); ?>
			<?php echo esc_attr( $icon_color ); ?>
			<?php echo esc_attr( $border_radius ); ?>
		}
		.wp-block-crosswinds-blocks-author-social-icons .social-icon a:hover,
		.wp-block-crosswinds-blocks-author-social-icons .social-icon a:focus {
			<?php echo esc_attr( $icon_hover_background_color ); ?>
			<?php echo esc_attr( $icon_hover_color ); ?>
		}
	</style>
	<?php
	$author_id = get_the_author_meta( 'ID' );
	$author    = get_userdata( $author_id );
	foreach ( $icons as $icon ) {
		if ( in_array( $icon['slug'], $attributes['socialIcons'] ) && ( ( '' !== get_user_meta( $author_id, $icon['slug'], true ) && null !== get_user_meta( $author_id, $icon['slug'], true ) ) || ( 'email' === $icon['slug'] && ( '' !== $author->user_email && null !== $author->user_email ) ) || ( 'website' === $icon['slug'] && ( '' !== $author->user_url && null !== $author->user_url ) ) ) ) {
			if ( 'email' === $icon['slug'] ) {
				$the_link = $author->user_email;
			} elseif ( 'website' === $icon['slug'] ) {
				$the_link = $author->user_url;
			} else {
				$the_link = get_user_meta( $author_id, $icon['slug'], true );
			}
			if ( 'show-label-icon' === $attributes['iconStyle'] ) {
				?>
				<div class="social-icon">
					<a class="<?php echo esc_attr( $icon['slug'] ); ?>" href="<?php echo esc_url( $the_link ); ?>" target="_blank"><span class="<?php echo esc_attr( $icon['icon'] ); ?>"></span><?php echo esc_html( $icon['name'] ); ?></a>
				</div>
				<?php
			} elseif ( 'show-icon' === $attributes['iconStyle'] ) {
				?>
				<div class="social-icon">
					<a class="<?php echo esc_attr( $icon['slug'] ); ?>" href="<?php echo esc_url( $the_link ); ?>" target="_blank"><span class="<?php echo esc_attr( $icon['icon'] ); ?>"><span class="screen-reader-text"><?php echo esc_html( $icon['name'] ); ?></span></span></a>
				</div>
				<?php
			} else {
				?>
				<div class="social-icon">
					<a class="<?php echo esc_attr( $icon['slug'] ); ?>" href="<?php echo esc_url( $the_link ); ?>" target="_blank"><?php echo esc_html( $icon['name'] ); ?></a>
				</div>
				<?php
			}
		}
	}
	?>
</div>
