<?php

/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              https://www.56degrees.co.uk/
 * @since             1.0.0
 * @package           Fsdsb_Simple_Banner
 *
 * @wordpress-plugin
 * Plugin Name:       Simple banner - Lightweight Announcement Banner Without jQuery
 * Plugin URI:        https://wordpress.org/plugins/fsd-simple-banner
 * Description:       Simple banner a lightweight WordPress plugin without jQuery, allows adding a simple banner for announcements on your site. Perfect for notifications.
 * Version:           1.2.2
 * Author:            56 Degrees
 * Author URI:        https://www.56degrees.co.uk/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       fsd-simple-banner
 * Domain Path:       /languages
 */

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

/**
 * Currently plugin version.
 * Start at version 1.0.0 and use SemVer - https://semver.org
 * Rename this for your plugin and update it as you release new versions.
 */
define( 'FSDSB_SIMPLE_BANNER_VERSION', '1.2.2' );
define( 'FSDSB_SIMPLE_BANNER_PATH', plugin_dir_path( __FILE__ ) );


/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-fsd-simple-banner-activator.php
 */
function fsdsb_activate_simple_banner() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-fsd-simple-banner-activator.php';
	Fsdsb_Simple_Banner_Activator::activate();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-fsd-simple-banner-deactivator.php
 */
function fsdsb_deactivate_simple_banner() {
	require_once plugin_dir_path( __FILE__ ) . 'includes/class-fsd-simple-banner-deactivator.php';
	Fsdsb_Simple_Banner_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'fsdsb_activate_simple_banner' );
register_deactivation_hook( __FILE__, 'fsdsb_deactivate_simple_banner' );

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path( __FILE__ ) . 'includes/class-fsd-simple-banner.php';

/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function fsdsb_run_simple_banner() {

	$plugin = new Fsdsb_Simple_Banner();
	$plugin->run();

}
fsdsb_run_simple_banner();

add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'fsd_simple_banner_plugin_action_links');

function fsd_simple_banner_plugin_action_links($links) {
    // Add the link to the settings page
    $settings_link = sprintf(
        '<a href="%s">%s</a>',
        esc_url(admin_url('admin.php?page=crb_carbon_fields_container_fsd-sb.php')),
        esc_html__('Settings', 'fsd-simple-banner')
    );

    // Prepend the settings link to the existing plugin action links
    array_push($links, $settings_link);

    return $links;
}


add_action('wp_footer', 'fsdsb_inject_shortcode_into_header');

function fsdsb_inject_shortcode_into_header() {
    // Output your shortcode
    echo do_shortcode(esc_html('[fsdsb_simple_banner]'));
}

add_shortcode("fsdsb_simple_banner", "fsdsb_render_simple_banner");


function fsdsb_show_banner($banner_settings) {
    // Check if banner should be shown based on conditions
    $show_banner = ($banner_settings['show_banner'] == 'yes');

    if(isset($_COOKIE['fsd-simple-banner']) && $_COOKIE['fsd-simple-banner'] == 'hidden') {
        $show_banner = false;
    }
    return $show_banner;

}

function fsdsb_render_simple_banner() {
    $current_page_id = get_the_ID();
    $cta_settings = include FSDSB_SIMPLE_BANNER_PATH . 'public/settings/fsd-simple-banner-cta-settings.php';
    $banner_settings = include FSDSB_SIMPLE_BANNER_PATH . 'public/settings/fsd-simple-banner-settings.php';
    $design_settings = include FSDSB_SIMPLE_BANNER_PATH . 'public/settings/fsd-simple-banner-design-settings.php';
    $content = include FSDSB_SIMPLE_BANNER_PATH . 'public/settings/fsd-simple-banner-content.php';
    $position = include FSDSB_SIMPLE_BANNER_PATH . 'public/settings/fsd-simple-banner-position-settings.php';
    $custom_css = carbon_get_theme_option( 'crb_fsd_sb_custom-css' );


    ob_start();
    if (fsdsb_show_banner($banner_settings)) :
            wp_enqueue_style('fsd-simple-banner-custom-css', plugins_url('public/css/fsd-simple-banner-public.css', __FILE__), false, FSDSB_SIMPLE_BANNER_VERSION);

            $banner_border_radius = $design_settings['banner_border_radius'] ? $design_settings['banner_border_radius'] . 'px' : '0px';
            $banner_border_width = ($design_settings['banner_border_width'] !== null && $design_settings['banner_border_width'] !== '') ? $design_settings['banner_border_width'] . 'px' : '0px';
            $banner_border_colour = ($design_settings['banner_border_colour']) ? $design_settings['banner_border_colour']  : 'white';
            $cta_border_radius = ($cta_settings['cta_border_radius']) ? $cta_settings['cta_border_radius'] . 'px' : '6px';
            $cta_border_width = ($cta_settings['cta_border_width'] !== null && $cta_settings['cta_border_width'] !== '') ? $cta_settings['cta_border_width'] . 'px' : '1px';
            $cta_border_colour = ($cta_settings['cta_border_colour']) ? $cta_settings['cta_border_colour']  : 'white';
            $cta_padding_y = ($cta_settings['cta_padding_y']) ? $cta_settings['cta_padding_y'] . 'px' : '10px';
            $cta_padding_x = ($cta_settings['cta_padding_x']) ? $cta_settings['cta_padding_x'] . 'px' : '10px';
            $cta_font_size = ($cta_settings['cta_font_size']) ? $cta_settings['cta_font_size'] . 'px' : '16px';

            $fsd_sb_custom_css = '
                :root {
                    --fsd-sb-wysiwyg-colour:  '. $design_settings['banner_text_colour'] .';
                    --fsd-sb-wysiwyg-font-size: '. $design_settings['banner_font_size'] .'px;
                    --fsd-sb-wysiwyg-link-colour: '. $design_settings['banner_link_colour'] .';
                    --fsd-sb-wysiwyg-link-colour-hover: '. $design_settings['banner_link_hover_colour'] .';
                    
                    --fsd-sb-background-colour: '. $design_settings['banner_colour'] .';
                    
                    --fsd-sb-border-radius: '. $banner_border_radius .';
                    --fsd-sb-border-width: '. $banner_border_width .';
                    --fsd-sb-border-colour: '. $banner_border_colour .';
                    --fsd-sb-position: '. $position['banner_position'] .';
                    --fsd-sb-padding: '. $design_settings['banner_padding'] .'px;
                    --fsd-sb-close-button-size: '. $design_settings['banner_close_btn_size'] .'px;
                    
                    --fsd-sb-cta-colour: '. $cta_settings['cta_background'] .';
                    --fsd-sb-cta-border-radius: '. $cta_border_radius .';
                    --fsd-sb-cta-border-width: '. $cta_border_width .';
                    --fsd-sb-cta-border-colour: '. $cta_border_colour .';
                    --fsd-sb-cta-padding: '. $cta_padding_y .' '. $cta_padding_x .';
                    
                    --fsd-sb-cta-font-size: '. $cta_font_size .';
                    --fsd-sb-cta-text-colour: '. $cta_settings['cta_text_colour'] .';
                    --fsd-sb-cta-text-colour-hover: '. $cta_settings['cta_text_colour_hover'] .';
                    --fsd-sb-cta-background-hover: '. $cta_settings['cta_background_hover'] .';
                    --fsd-sb-cta-border-colour-hover: '. $cta_settings['cta_border_colour_hover'] .';
                }
                '. esc_html($custom_css) .'
            ';
            wp_add_inline_style( 'fsd-simple-banner-custom-css', esc_html($fsd_sb_custom_css) );
        ?>
        <div class="fsd-sb-info-banner <?php echo esc_html($position['banner_placement']); ?> <?php echo ($design_settings['banner_shadow'] === 'yes') ? 'info-banner--shadow' : '' ?>">
            <div class="fsd-sb-info-banner__wrapper">
                <div class="fsd-sb-info-banner__wysiwyg">
                    <?php echo wp_kses_post(apply_filters( 'fsd_simple_banner_filter', $content['banner_text'])); ?>
                </div>
                <?php
                if ($banner_settings['show_banner_close_button']) {
                    include(FSDSB_SIMPLE_BANNER_PATH . 'public/partials/fsd-simple-banner-close-btn.php');
                }
                if($cta_settings['cta_show']) {
                    include(FSDSB_SIMPLE_BANNER_PATH . 'public/partials/fsd-simple-banner-cta-button.php');
                }
                ?>
            </div>
        </div>
    <?php
    endif;
    return ob_get_clean();
}
