<?php
/**
 * Contains various functions that may be potentially used throughout
 * the theme.
 *
 * @package Hotel 26
 * @author  hotel 26 Software
 * @since   1.0.0
 */

/**
 * Do not allow direct script access.
 */
if (! defined('ABSPATH') ) {
    exit;
}

/**
 * Check if we're on a hotel 26 admin page.
 *
 * @since  1.0.0
 * @param  boolean|string $base current screen base.
 * @param  string         $slug page slug.
 * @return boolean
 */
function hotel_26_admin_page( $base = false, $slug = 'hotel-26' )
{

    if (false === $base ) {
        $base = get_current_screen()->base;
    }

    return false !== strpos($base, $slug);
}

/**
 * Print admin notice.
 *
 * @since  1.0.0
 * @param  array $args array of options.
 * @return boolean|void
 */
function hotel_26_print_notice( $args )
{

    $defaults = array(
    'type'           => 'success',
    'message'        => '',
    'is_dismissible' => true,
    'message_id'     => '',
    'expires'        => 0,
    'display_on'     => array(),
    'action_link'    => '',
    'action_text'    => '',
    'dismiss_text'   => esc_html__('Dismiss', 'hotel-26'),
    );

    $args = wp_parse_args($args, $defaults);

    if (hotel_26_is_notice_dismissed($args['message_id']) ) {
        return false;
    }

    if (! empty($args['display_on']) ) {

        $base    = get_current_screen()->base;
        $display = false;

        foreach ( $args['display_on'] as $page ) {
            if (false !== strpos($base, $page) ) {
                $display = true;
            }
        }

        if (! $display ) {
            return false;
        }
    }

    $hotel_26_is_dismissible = $args['is_dismissible'] ? ' is-dismissible' : ''; ?>

    <div id="<?php echo esc_attr($args['message_id']); ?>" class="notice hotel-26-notice notice-<?php echo esc_attr($args['type']); ?> <?php echo esc_attr($hotel_26_is_dismissible); ?>">
        <div class="hotel-26-notice-container">
            <div class="hotel-26-notice-content">
                <h2><?php echo ( wp_kses($args['message'], hotel_26_get_allowed_html_tags()) ); ?></h2>
                <?php 
                printf(
                        /* translators: 1: opening anchor tag, 2: closing anchor tag */
                    wp_kses(__('%1$sGo to Recommended Plugins%2$s', 'hotel-26'), hotel_26_get_allowed_html_tags()), '<a class="hotel-26-btn-get-started button button-primary button-hero hotel-26-button-padding" href="' . esc_url(admin_url('themes.php?page=hotel-26-dashboard')) . '">', '</a>'
                );
                
                if ($args['action_link'] && $args['action_text'] ) {
                    ?>
                    <p class="hotel-26-notice-action hotel-26-buttons plugins">
                    <?php
                        
                    $class       = ' active button button-primary button-hero';
                    $button_text = __('Start Demo Import', 'hotel-26');
                    $link        = admin_url('admin.php?page=one-click-demo-import');
                    $data        = '';

                    printf(
                        '<a class="%1$s" %2$s %3$s role="button"> %4$s </a>',
                        esc_attr($class),
                        isset($link) ? 'href="' . esc_url($link) . '"' : '',
							$data, // phpcs:ignore
                        esc_html($button_text)
                    );
                    ?>

                    <?php
                    if ($args['dismiss_text'] ) {
                        ?>
                            <a href="#" class="hotel-26-btn hotel-26-btn-outline hotel-26-btn secondary button button-secondary hotel-26-notice-dismiss button-hero" role="button"><?php echo esc_html($args['dismiss_text']); ?></a>
                        <?php
                    }
                    ?>
                    </p><!-- END .hotel-26-notice-action -->
                    <?php
                }
                ?>
            </div>
            <img class="hotel-26-screenshot" src="<?php echo esc_url(get_stylesheet_directory_uri().'/assets/images/notice.jpg')?>" alt="<?php esc_attr_e('Hotel 26', 'hotel-26'); ?>" />
        </div>
    </div>    

    <script type="text/javascript">
        jQuery( document ).ready( function ( $ ) {

            var msgid = "<?php echo esc_attr($args['message_id']); ?>";
            var $el   = $( '#' + msgid );

            $el.on( 'click', '.notice-dismiss, .hotel-26-notice-dismiss', function ( event ) {

                var expires = "<?php echo esc_attr($args['expires']); ?>";
                var nonce = "<?php echo esc_attr(wp_create_nonce('hotel_26_dismiss_notice')); ?>";

                $.post( ajaxurl, {
                    action:         'hotel_26_dismiss_notice',
                    msgid:             msgid,
                    expires:         expires,
                    _ajax_nonce:     nonce,
                } );

                $el.fadeTo( 100, 0, function() {
                    $el.slideUp( 100, function() {
                        $el.remove();
                    });
                });
            } );
        } );
    </script>
    <?php
}

/**
 * Check if admin notice is dismissed.
 *
 * @since  1.0.0
 * @param  array $id Notice ID.
 * @return boolean
 */
function hotel_26_is_notice_dismissed( $id )
{

    if (false !== get_transient('hotel_26_notice_' . $id) ) {
        return true;
    }

    return false;
}

/**
 * Ajax handler to dismiss admin notice.
 *
 * @since  1.0.0
 * @return void
 */
function hotel_26_dismiss_notice()
{

    check_ajax_referer('hotel_26_dismiss_notice');

    if (! isset($_POST['msgid']) ) {
        die;
    }

    $message_id = sanitize_text_field(wp_unslash($_POST['msgid']));
    $expires    = isset($post['expires']) ? intval($post['expires']) : 0;

    $message              = (array) get_transient('hotel_26_notice_' . $message_id);
    $message['time']      = time();
    $message['dismissed'] = true;

    set_transient('hotel_26_notice_' . $message_id, $message, $expires);
    die;
}
add_action('wp_ajax_hotel_26_dismiss_notice', 'hotel_26_dismiss_notice');