<?php
defined('ABSPATH') or die('No script kiddies please!!');
if (!class_exists('STD_Shortcode')) {

    class STD_Shortcode extends STD_Library {

        function __construct() {
            add_shortcode('std', array($this, 'generate_shortcode_output'));

            add_action('wp_footer', array($this, 'append_subscription_form'));
            add_action('std_after_form', array($this, 'append_google_reCaptcha'));
        }

        function generate_shortcode_output($atts) {
            wp_enqueue_style('std-frontend-custom', STD_CSS_DIR . '/std-custom.css', array(), STD_VERSION);
            if (isset($atts['alias'])) {
                $alias = $atts['alias'];
                $form_row = $this->get_form_row_by_alias($alias);
                if (!empty($form_row) && $form_row->form_status == 1) {
                    ob_start();
                    include(STD_PATH . 'inc/views/frontend/std-shortcode.php');
                    $form_html = ob_get_contents();
                    ob_clean();
                    ob_end_flush();
                    return $form_html;
                }
            } else {
                return '';
            }
        }

        function append_subscription_form() {

            if (!isset($_GET['std_preview'])) {
                wp_reset_query();
                global $wp_query;
                if (!empty($wp_query->queried_object->ID)) {
                    $std_metabox_details = get_post_meta($wp_query->queried_object->ID, '_std_metabox_details', true);
                    if (!empty($std_metabox_details['enable_popup']) && !empty($std_metabox_details['form_alias'])) {
                        if (!empty($std_metabox_details['disabled_already_subscriber']) && $this->is_already_subscribed()) {
                            return;
                        }
                        $delay = $std_metabox_details['delay'];
                        $form_alias = $std_metabox_details['form_alias'];
                        if (!empty($std_metabox_details['disable_when_closed']) && isset($_COOKIE['std_popup_hide_flag_' . $form_alias])) {
                            return;
                        }
?>
                        <div class="std-popup-innerwrap std-delay-popup" style="display:none;" data-delay="<?php echo intval($delay); ?>" data-alias="<?php echo esc_attr($form_alias); ?>">
                            <div class="std-overlay std-popup-wrapper">
                                <div class="std-popup-contetn-wrap">
                                    <a href="javascript:void(0)" class="std-popup-close <?php echo (!empty($std_metabox_details['disable_when_closed'])) ? 'std-disable-when-closed-trigger' : ''; ?>" data-alias="<?php echo esc_attr($form_alias); ?>" data-hide-days="<?php echo apply_filters('std_popup_hide_days', 365); ?>"><i class="fas fa-times"></i></a>

                                    <?php echo do_shortcode('[std alias="' . esc_attr($form_alias) . '"]');
                                    ?>
                                </div>
                            </div>
                        </div>

                        <?php
                    }
                } else {
                    if (is_home()) {
                        $std_settings = get_option('std_settings');
                        if (!empty($std_settings['extra']['enable_popup_index_page']) && !empty($std_settings['extra']['form_alias'])) {
                            $delay = $std_settings['extra']['delay'];
                            $form_alias = $std_settings['extra']['form_alias'];
                        ?>
                            <div class="std-popup-innerwrap std-delay-popup" style="display:none;" data-delay="<?php echo intval($delay); ?>">
                                <div class="std-overlay std-popup-wrapper">
                                    <div class="std-popup-contetn-wrap">
                                        <a href="javascript:void(0)" class="std-popup-close"><i class="fas fa-times"></i></a>
                                        <?php echo do_shortcode('[std alias="' . esc_attr($form_alias) . '"]');
                                        ?>
                                    </div>
                                </div>
                            </div>
<?php
                        }
                    }
                }
            }
        }

        function append_google_reCaptcha($form_row) {
            include(STD_PATH . 'inc/views/frontend/google-reCaptcha.php');
        }
    }

    new STD_Shortcode();
}
