<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;

$embed_code = isset($campoal_options['conikal_embed_code_field']) ? $campoal_options['conikal_embed_code_field'] : 0;
$social_buttons_sortable = isset($campoal_options['conikal_social_buttons_sortable_field']) ? $campoal_options['conikal_social_buttons_sortable_field'] : 0;
$fb_app_id = isset($campoal_options['conikal_fb_id_field']) ? $campoal_options['conikal_fb_id_field'] : '';
$fb_login = isset($campoal_options['conikal_fb_login_field']) ? $campoal_options['conikal_fb_login_field'] : 0;

// Get social meta
$social_meta = conikal_social_meta();

// Get social button data
$social_buttons = conikal_social_buttons_data($social_meta, ($fb_login && !empty($fb_app_id) ? $fb_app_id : false));

// column button
$share_column_number = 5;
$share_column_class  = 'six';
if (wp_is_mobile()) {
    $share_column_number = 4;
    $share_column_class  = 'five';
}

$share_classes =  esc_attr($share_column_class) . ' column grid social-share';
$button_classes = 'basic icon button';

$mail_sharing_subject   = conikal_user_email_sharing($social_meta->id, 'subject');
$mail_sharing_content   = conikal_user_email_sharing($social_meta->id, 'message');
$mail_sharing_custom    = conikal_user_email_sharing($social_meta->id, 'custom');
?>

    <div class="ui <?php echo esc_attr($share_classes) ?>">
        <?php 
            $i = 1;
            foreach ($social_buttons_sortable as $social => $status) {
                if ($status && $i <= $share_column_number && array_key_exists($social, $social_buttons)) {
                    $button_data = $social_buttons[$social]; ?>

                    <div class="column">
                        <a href="<?php echo esc_attr($button_data['onclick'] ? $button_data['link'] : $button_data['link']) ?>" 
                            class="ui <?php echo esc_attr($button_classes) ?> <?php echo esc_attr(in_array($social, array('facebook', 'messenger')) ? '' : 'share-button-counter ') ?><?php echo esc_attr($button_data['classes']) ?>" 
                            id="<?php echo esc_attr($social . '-share-button' . $social_meta->id) ?>" 
                            <?php if ($button_data['onclick']) : ?>
                            onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=<?php echo esc_attr($button_data['popup']['height']) ?>,width=<?php echo esc_attr($button_data['popup']['width']) ?>');return false;"
                            <?php endif; ?>
                            data-social="<?php echo esc_attr($social) ?>" 
                            data-content="<?php echo esc_attr($button_data['label']) ?><?php echo sprintf(esc_html__(' (%s)', 'campoal'), esc_attr($button_data['counter'])) ?>" 
                            data-variation="small" 
                            data-position="top center" 
                            data-id="<?php echo esc_attr($social_meta->id) ?>" 
                            data-heading="<?php echo esc_attr(urlencode($social_meta->title)) ?>" 
                            data-link="<?php echo esc_attr($social_meta->link) ?>" 
                            <?php if ($social == 'mail' || $social == 'gmail') : ?>
                            data-subject="<?php echo esc_attr($mail_sharing_subject) ?>" 
                            data-mail="<?php echo esc_attr($mail_sharing_content) ?>" 
                            data-custom="<?php echo esc_attr($mail_sharing_custom) ?>" 
                            <?php endif; ?>>
                                <i class="<?php echo esc_attr($button_data['icon']) ?>"></i>
                        </a>
                    </div>
                <?php 
                }
                $i++;
            }
        ?>

        <div class="column">
            <div class="ui icon top right pointing dropdown <?php echo esc_attr($button_classes) ?> more-share">
                <i class="ellipsis horizontal icon"></i>
                <div class="menu">
                    <?php 
                        $i_dropdown = 1;
                        foreach ($social_buttons_sortable as $social => $status) {
                            if ($status && $i_dropdown > $share_column_number && array_key_exists($social, $social_buttons)) {
                                $button_data = $social_buttons[$social];
                                ?>
                                    <a href="<?php echo esc_attr($button_data['onclick'] ? $button_data['link'] : $button_data['link']) ?>" 
                                        class="item <?php echo esc_attr(in_array($social, array('facebook', 'messenger')) ? '' : 'share-button-counter ') ?><?php echo esc_attr($button_data['classes']) ?>" 
                                        id="<?php echo esc_attr($social . '-share-button' . $social_meta->id) ?>" 
                                        <?php if ($button_data['onclick']) : ?>
                                        onclick="javascript:window.open(this.href, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=<?php echo esc_attr($button_data['popup']['height']) ?>,width=<?php echo esc_attr($button_data['popup']['width']) ?>');return false;"
                                        <?php endif; ?>
                                        data-social="<?php echo esc_attr($social) ?>" 
                                        data-id="<?php echo esc_attr($social_meta->id) ?>" 
                                        data-heading="<?php echo esc_attr(urlencode($social_meta->title)) ?>" 
                                        data-link="<?php echo esc_attr($social_meta->link) ?>" 
                                        <?php if ($social == 'mail' || $social == 'gmail') : ?>
                                        data-subject="<?php echo esc_attr($mail_sharing_subject) ?>" 
                                        data-mail="<?php echo esc_attr($mail_sharing_content) ?>" 
                                        data-custom="<?php echo esc_attr($mail_sharing_custom) ?>" 
                                        <?php endif; ?>>
                                            <i class="<?php echo esc_attr($button_data['icon']) ?>"></i>
                                            <?php echo esc_attr($button_data['name']) ?>
                                            <span id="<?php echo esc_attr($social) ?>-share-counter<?php echo esc_attr($social_meta->id) ?>">
                                                <?php echo sprintf(esc_html__(' (%s)', 'campoal'), esc_html( $button_data['counter']) ) ?>
                                            </span>
                                    </a>
                            <?php 
                            }
                            $i_dropdown++;
                        }
                    ?>
                </div>
            </div>
        </div>
    </div>

    <?php if (!empty($embed_code)) {
        get_template_part('templates/embed/embed-modal');
    } ?>
