<?php
/**
 * Abandoned Cart Lite for WooCommerce
 *
 * Admin notices handler class.
 *
 * @author  Tyche Softwares
 * @package abandoned-cart-lite
 * @since 5.6 file name changed from wcal_actions.php to class-wcal-delete-handler
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * It will display the admin notices for the pro version.
 *
 * @author  Tyche Softwares
 */
class Wcal_Admin_Notice {

	/**
	 * Show a DB Update Notice when upgrading from 4.7 to latest version
	 *
	 * @since 4.8
	 *
	 * @hook admin_notices
	 */
	public static function wcal_show_db_update_notice() {

		if ( isset( $_GET['page'] ) && 'woocommerce_ac_page' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification

			if ( ! get_option( 'wcal_scheduler_update_dismiss', false ) ) {
				$post_link = 'https://www.tychesoftwares.com/moving-to-the-action-scheduler-library/?utm_source=AcLiteNotice&utm_medium=link&utm_campaign=AbandonCartLite';
				?>
					<div id='wcal_cron_notice' class='is-dismissible notice notice-info wcal-cron-notice'>
						<p>
						<?php
						printf(
							// Translators: Plugin Name and URL.
							__( 'The %1$s now uses the Action Scheduler library to send reminders. For further details, please visit <a href="%2$s" target="_blank">here</a>.', 'woocommerce-abandoned-cart' ), // phpcs:ignore.
							wp_kses_post( '<b>Abandoned Cart Lite for WooCommerce</b>' ),
							$post_link // phpcs:ignore.
						);
						?>
						</p>
					</div>
				<?php
			}
		}

		if ( isset( $_GET['ac_update'] ) && 'email_templates' === $_GET['ac_update'] ) { // phpcs:ignore WordPress.Security.NonceVerification
			return;
		}

		global $wpdb;

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery
		$results = $wpdb->get_results(
			'SHOW FULL COLUMNS FROM `' . $wpdb->prefix . 'ac_email_templates_lite` WHERE Field = "subject" OR Field = "body"'
		);

		$update_url = wp_nonce_url(
			admin_url( 'admin.php?page=woocommerce_ac_page&action=listcart&ac_update=email_templates' ),
			'wcal_update_email_templates'
		);

		foreach ( $results as $key => $value ) {
			if ( 'utf8mb4_unicode_ci' !== $value->Collation ) { // phpcs:ignore
				?>
				<div id="wcal_update" class="updated woocommerce-message" style="padding:15px;">
					<span>
						<?php echo esc_html__( 'We need to update your email template database for some improvements. Please take a backup of your databases for your piece of mind.', 'woocommerce-abandoned-cart' ); ?>
					</span>
					<span class="submit">
						<a href="<?php echo esc_url( $update_url ); ?>" class="button-primary" style="float:right;"><?php echo esc_html__( 'Update', 'woocommerce-abandoned-cart' ); ?></a>
					</span>
				</div>
				<?php
				break;
			}
		}
	}
	/**
	 * Enabling this setting will allow users registered on the website to access it via reminder email links without needing to login
	 *
	 * @since 5.16.0
	 *
	 * @hook admin_notices
	 */
	public static function wcal_show_auto_loggin_notice() {

		if ( isset( $_GET['page'] ) && 'woocommerce_ac_page' === $_GET['page'] && isset( $_GET['action'] ) && 'emailsettings' === $_GET['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification

			if ( ! get_option( 'wcal_auto_login_notice_dismiss', false ) ) {
				if ( 'on' === get_option( 'wcal_auto_login_users', 'on' ) ) {
					?>
						<div id='wcal_auto_login_notice' class='is-dismissible notice notice-info wcal-cron-notice'>
							<p>
							<?php
							printf(
							// Translators: Plugin Name and URL.
								esc_html__( 'Enabling this setting will allow users registered on the website to access it via reminder email links without needing to login, which may be a security vulnerability.', 'woocommerce-abandoned-cart' ),
								wp_kses_post( '<b>Abandoned Cart Lite for WooCommerce</b>' )
							);
							?>
							</p>
						</div>
					<?php
				}
			}
		}
	}
}
