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

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

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

	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 electromix_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', 'electromix' ),
	);

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

	if ( electromix_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;
		}
	}

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

	<div id="<?php echo esc_attr( $args['message_id'] ); ?>" class="notice electromix-notice notice-<?php echo esc_attr( $args['type'] ); ?> <?php echo esc_attr( $electromix_is_dismissible ); ?>">
		<div class="electromix-notice-container">
			<div class="electromix-notice-content">
				<h2><?php echo ( wp_kses( $args['message'], electromix_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', 'electromix'),electromix_get_allowed_html_tags() ), '<a class="electromix-btn-get-started button button-primary button-hero electromix-button-padding" href="' . esc_url( admin_url('themes.php?page=electromix-dashboard') ) . '">', '</a>'
					);
				
				if ( $args['action_link'] && $args['action_text'] ) {
					?>
					<p class="electromix-notice-action electromix-buttons plugins">
					<?php
						
						$class       = ' active button button-primary button-hero';
						$button_text = __( 'Start Demo Import', 'electromix' );
						$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="electromix-btn electromix-btn-outline electromix-btn secondary button button-secondary electromix-notice-dismiss button-hero" role="button"><?php echo esc_html( $args['dismiss_text'] ); ?></a>
							<?php
						}
						?>
					</p><!-- END .electromix-notice-action -->
					<?php
				}
				?>
			</div>
			<img class="electromix-screenshot" src="<?php echo esc_url(get_stylesheet_directory_uri().'/assets/images/notice.jpg' )?>" alt="<?php esc_attr_e( 'Electromix', 'electromix' ); ?>" />
		</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, .electromix-notice-dismiss', function ( event ) {

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

				$.post( ajaxurl, {
					action: 		'electromix_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 electromix_is_notice_dismissed( $id ) {

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

	return false;
}

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

	check_ajax_referer( 'electromix_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( 'electromix_notice_' . $message_id );
	$message['time']      = time();
	$message['dismissed'] = true;

	set_transient( 'electromix_notice_' . $message_id, $message, $expires );
	die;
}
add_action( 'wp_ajax_electromix_dismiss_notice', 'electromix_dismiss_notice' );