<?php
/**
 * Abstract base class for Smart Post Show Blocks.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/blocks/includes
 */

namespace SmartPostShow\Blocks;

use SmartPostShow\Blocks\Block_Base;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Social Social Item Block Class.
 */
class Post_Social_Share extends Block_Base {


	/**
	 * Set_block_properties
	 *
	 * @return void
	 */
	protected function set_block_properties() {
		$this->block_name = 'post-social-share';
		$this->styles     = array( 'sp_smart_post_blocks_google_fonts', 'sp_smart_post_blocks_social_icons_style' );
	}

	/**
	 * Render Social Profile Item block.
	 *
	 * @param array  $attributes Block attributes.
	 * @param string $content Block content.
	 * @param array  $blocks Inner blocks.
	 * @return string        Rendered HTML.
	 */
	public function render_block( $attributes, $content = '', $blocks = array() ) {
		if ( \SmartPostShow\Blocks\Helper::is_editor_page() ) {
			return $content;
		}

		$is_license_activated = $this->is_license_activated();
		if ( 'valid' !== $is_license_activated ) {
			return $is_license_activated;
		}
		$post_id                 = get_the_ID();
		$permalink               = get_permalink( $post_id );
		$sp_block_name           = $attributes['blockName'] ?? '';
		$sp_unique_id            = $attributes['uniqueId'] ?? '';
		$sp_unique_id            = $attributes['uniqueId'] ?? '';
		$sp_layout               = $attributes['layout'] ?? '';
		$social_list             = $attributes['socialSharingMedia'] ?? '';
		$label_global_typography = $attributes['labelGlobalTypography'] ?? '';
		$label_global_class      = $label_global_typography['class'] ?? '';
		$social_orientation      = $attributes['socialOrientation'] ?? '';
		$additional_id           = $attributes['additionalId'] ?? '';
		$display_control         = $attributes['displayControl'] ?? '';
		$enable_label            = $attributes['socialMediaLabel'] ?? '';
		$separator_enable        = $attributes['separatorEnable'] ?? '';
		$additional_class        = $attributes['additionalCssClass'] ?? '';

		$social_sharing_links = Helper::social_share_link( $permalink );

		$share_icon = Helper::get_icon_list( 'share-nodes' );

		ob_start();
		?>
		<div id="<?php echo esc_attr( $additional_id ); ?>" class="sp-smart-post-wrapper sp-smart-post-social-share-<?php echo esc_attr( $display_control ); ?> <?php echo esc_attr( $additional_class ); ?>">
			<div id="<?php echo esc_attr( $sp_unique_id ); ?>" class="sp-smart-post-social-share sp-smart-post-social-share-<?php echo esc_attr( $social_orientation ); ?>">
				<ul class="sp-smart-post-social-share-links">
					<li>
						<div class="sp-smart-post-social-share-count-area">
							<i class="sp-smart-post-social-share-count-area-icon">
								<?php if ( ! empty( $share_icon ) ) { ?>
							<svg width="24" height="24" viewBox="<?php echo esc_attr( $share_icon['viewBox'] ); ?>" fill="none" xmlns="http://www.w3.org/2000/svg">
									<?php
									if ( is_array( $share_icon['path'] ) && ! empty( $icon['path'] ) ) {
										foreach ( $share_icon['path'] as $path ) {
											?>
										<path d="<?php echo esc_attr( $path ); ?>" fill="currentColor"></path>
											<?php
										}
									} else {
										?>
									<path d="<?php echo esc_attr( $share_icon['path'] ); ?>" fill="currentColor"></path>
									<?php } ?>
							</svg>
						<?php } ?>
							</i>
							<span class="sp-smart-post-social-share-count-area-label">Shares</span>
						</div>
					</li>
					<?php
					if ( ! empty( $social_list ) ) {
						foreach ( $social_list as $social ) {
							$label = $social['label'] ?? '';
							$value = $social['value'] ?? '';
							?>
						<li>
							<a class="<?php echo esc_attr( $label_global_class ); ?>" target="_blank" href="<?php echo esc_url( $social_sharing_links[ $value ] ); ?>">
								<span class="sp-smart-post-social-share-icon sp-icon-<?php echo esc_attr( $value ); ?>"></span>
								<?php if ( $enable_label && $separator_enable ) { ?>
									<span class="sp-smart-post-social-share-divider"></span>
								<?php } ?>
								<?php if ( $enable_label ) { ?>
									<span class="sp-smart-post-social-share-label"><?php echo esc_html( $label ); ?></span>
								<?php } ?>
							</a>
						</li>
							<?php
						}
					}
					?>
				</ul>
			</div>
		</div>
		<?php
		return ob_get_clean();
	}
}
