<?php
/**
 * Smart Offers Admin Notifications
 *
 * @package    smart-offers/includes/admin/
 * @since      3.10.7
 * @version    1.2.3
 */

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

if ( ! class_exists( 'SO_Admin_Notifications' ) ) {

	/**
	 * Class for handling admin notifications of Smart Offers
	 */
	class SO_Admin_Notifications {

		/**
		 * Constructor
		 */
		public function __construct() {

			add_filter( 'plugin_action_links_' . plugin_basename( SO_PLUGIN_FILE ), array( $this, 'plugin_action_links' ) );

			// Update footer text.
			add_filter( 'admin_footer_text', array( $this, 'so_footer_text' ) );
			add_filter( 'update_footer', array( $this, 'so_update_footer_text' ), 99 );

			// Filter to add Quick Help Widget.
			add_filter( 'sa_active_plugins_for_quick_help', array( $this, 'so_active_plugins_for_quick_help' ), 10, 2 );

			add_filter( 'sa_is_page_for_notifications', array( $this, 'is_page_for_notifications' ), 10, 2 );

			add_action( 'admin_notices', array( $this, 'so_show_admin_notices' ) );
			add_action( 'admin_init', array( $this, 'so_dismiss_admin_notice' ) );

			$this->may_be_show_sa_in_app_offer();

		}

		/**
		 * Function to add more action on plugins page
		 *
		 * @param array $links Existing links.
		 * @return array $links
		 */
		public function plugin_action_links( $links ) {

			$action_links = array(
				'getting_started' => '<a href="' . esc_url( add_query_arg( array( 'page' => 'so-about' ), 'edit.php?post_type=smart_offers' ) ) . '" title="' . esc_attr( __( 'Getting Started', 'smart-offers' ) ) . '" target="_blank">' . __( 'Getting Started', 'smart-offers' ) . '</a>',
				'settings'        => '<a href="' . esc_url( add_query_arg( array( 'page' => 'so-settings' ), 'edit.php?post_type=smart_offers' ) ) . '" title="' . esc_attr( __( 'View Smart Offers Settings', 'smart-offers' ) ) . '" target="_blank">' . __( 'Settings', 'smart-offers' ) . '</a>',
				'need_help'       => '<a href="' . esc_url( add_query_arg( array( 'page' => 'so-faqs' ), 'edit.php?post_type=smart_offers' ) ) . '" title="' . __( 'FAQ\'s', 'smart-offers' ) . '" target="_blank">' . __( 'FAQ\'s', 'smart-offers' ) . '</a>',
			);

			return array_merge( $action_links, $links );

		}

		/**
		 * Function to change footer text on Smart Offers pages in admin
		 *
		 * @param  string $so_footer_text Text in footer (left).
		 * @return string $so_footer_text
		 */
		public function so_footer_text( $so_footer_text ) {

			global $post, $pagenow;
			$get_page = ( ! empty( $_GET['page'] ) ) ? wc_clean( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore

			if ( ! empty( $pagenow ) ) {
				if (
					( is_object( $post ) && 'smart_offers' === $post->post_type )
					||
					( ! empty( $get_page ) && ( 'so-settings' === $get_page || 'so-about' === $get_page || 'so-shortcode' === $get_page || 'so-faqs' === $get_page || 'sa-wc-buy-now' === $get_page ) )
				) {
					$so_footer_text = __( 'Thank you for using <span style="color: #5850EC;">Smart Offers</span>. A huge thank you from <span style="color: #5850EC;">StoreApps</span>!', 'smart-offers' );
				}
			}

			return $so_footer_text;

		}

		/**
		 * Function to change footer text on Smart Offers pages in admin
		 *
		 * @param  string $so_text Text in footer (right).
		 * @return string $so_text
		 */
		public function so_update_footer_text( $so_text ) {

			global $post, $pagenow;

			$so_plugin_data     = get_plugin_data( WP_PLUGIN_DIR . '/smart-offers/smart-offers.php' );
			$so_current_version = $so_plugin_data['Version'];

			$get_post_type = ( ! empty( $_GET['post_type'] ) ) ? wc_clean( wp_unslash( $_GET['post_type'] ) ) : ''; // phpcs:ignore
			$get_page      = ( ! empty( $_GET['page'] ) ) ? wc_clean( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore

			if ( ! empty( $pagenow ) ) {
				if (
					( is_object( $post ) && 'smart_offers' === $post->post_type ) || ( ! empty( $get_post_type ) && ( 'smart_offers' === $get_post_type ) )
					||
					( ! empty( $get_page ) && ( 'so-settings' === $get_page || 'so-about' === $get_page || 'so-shortcode' === $get_page || 'so-faqs' === $get_page || 'sa-wc-buy-now' === $get_page ) )
				) {
					/* translators: %s: Current version of Smart Offers */
					$so_text = sprintf( __( 'Smart Offers version: <span style="color: #5850EC;">%s</span></strong>', 'smart-offers' ), $so_current_version );

				}
			}

			return $so_text;

		}

		/**
		 * Check if the page is valid for the notifications (Eg, Quick Help Widget)
		 *
		 * @param  array    $active_plugins Active plugins.
		 * @param  stdclass $upgrader       The upgrader object.
		 * @return array Active plugins
		 */
		public function so_active_plugins_for_quick_help( $active_plugins = array(), $upgrader = null ) {

			global $post;
			$get_post_type = ( ! empty( $_GET['post_type'] ) ) ? wc_clean( wp_unslash( $_GET['post_type'] ) ) : ''; // phpcs:ignore
			$get_page      = ( ! empty( $_GET['page'] ) ) ? wc_clean( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore

			if ( ( is_object( $post ) && 'smart_offers' === $post->post_type ) || ( ! empty( $get_post_type ) && ( 'smart_offers' === $get_post_type ) )
				||
				( ! empty( $get_page ) && ( 'so-settings' === $get_page || 'so-about' === $get_page || 'so-shortcode' === $get_page || 'so-faqs' === $get_page || 'sa-wc-buy-now' === $get_page ) )
			) {
				$active_plugins['so'] = 'smart-offers';
			} elseif ( array_key_exists( 'so', $active_plugins ) ) {
				unset( $active_plugins['so'] );
			}

			return $active_plugins;
		}

		/**
		 * To determine whether to show notification on a page or not
		 *
		 * @param bool  $bool Whether to show on current page True/false.
		 * @param mixed $upgrader StoreApps upgrader object.
		 * @return bool $bool True/false.
		 */
		public function is_page_for_notifications( $bool = false, $upgrader = null ) {

			$active_plugins = apply_filters( 'sa_active_plugins_for_quick_help', array(), $upgrader );
			if ( array_key_exists( $upgrader->sku, $active_plugins ) ) {
				return true;
			}

			return $bool;

		}

		/**
		 * Function to show admin notice on Smart Offers pages in admin
		 */
		public function so_show_admin_notices() {
			$bn_notice_status = get_option( 'so_bn_merged_notice_smart_offers', 'no' );
			if ( 'yes' === $bn_notice_status ) {
				return;
			}

			$is_bn_deactivated = get_option( 'so_wc_bn_deactivated', 'no' );

			global $post, $pagenow;
			$get_post_type = ( ! empty( $_GET['post_type'] ) ) ? wc_clean( wp_unslash( $_GET['post_type'] ) ) : ''; // phpcs:ignore
			$get_page      = ( ! empty( $_GET['page'] ) ) ? wc_clean( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore

			if ( ! empty( $pagenow ) ) {
				if (
					( is_object( $post ) && 'smart_offers' === $post->post_type ) || ( ! empty( $get_post_type ) && ( 'smart_offers' === $get_post_type ) )
					||
					( ! empty( $get_page ) && ( 'so-settings' === $get_page ) )
					||
					( 'plugins.php' === $pagenow )
				) {
					?>
					<style type="text/css" class="so-admin-notice">
						.so-notice {
							padding: 1.5em 1em !important;
						}
						.so-main-headline {
							font-size: 2em;
							font-weight: bold;
							padding-bottom: 0.4em;
							color: #5850EC;
						}
						.so-feature {
							font-size: 1.2em;
							line-height: 1em;
							padding: 1em 0;
						}
						.so-sub-content {
							font-size: 1.5em;
							line-height: 1.3em;
							padding-bottom: 1em;
						}
						.so-bn-deactivated {
							padding-bottom: 0.5em;
						}
						a.so-admin-btn-secondary {
							text-decoration: none;
							color: #aeb3b5;
							float: right;
							font-size: small;
							margin-top: -1em;
						}
					</style>
					<?php
						$plugin_data = SA_Smart_Offers::get_smart_offers_plugin_data();
						$version     = $plugin_data['Version'];
					?>
					<div class="updated success so-notice">
						<div class="so-main-headline"><?php echo sprintf( esc_html__( 'Welcome to Smart Offers %s!', 'smart-offers' ), $version ); ?></div>
						<div class="so-feature"><?php echo esc_html__( 'Thank you for updating to the latest version.', 'smart-offers' ); ?></div>
						<?php
							if ( 'yes' === $is_bn_deactivated ) {
								?>
								<div class="so-bn-deactivated">
									<?php
										echo __( 'Note - WooCommerce Buy Now plugin has been deactivated as it is now part of Smart Offers plugin. But all its Settings are preserved. So you can safely delete Buy Now plugin from the Plugins page.', 'smart-offers' );
									?>
								</div>
								<?php
							}
						?>
						<a class="so-admin-btn-secondary" href="?so_dismiss_admin_notice=1&option_name=so_bn_merged_notice&set=yes"><?php echo esc_html__( 'Hide', 'smart-offers' ); ?></a>
					</div>
					<?php
				}
			}
		}

		/**
		 * Function to dismiss any admin notice in Smart Offers
		 */
		public function so_dismiss_admin_notice() {

			$so_dismiss_admin_notice = ( ! empty( $_GET['so_dismiss_admin_notice'] ) ) ? wc_clean( wp_unslash( $_GET['so_dismiss_admin_notice'] ) ) : ''; // phpcs:ignore
			$so_option_name          = ( ! empty( $_GET['option_name'] ) ) ? wc_clean( wp_unslash( $_GET['option_name'] ) ) : ''; // phpcs:ignore
			$so_option_value         = ( ! empty( $_GET['set'] ) ) ? wc_clean( wp_unslash( $_GET['set'] ) ) : ''; // phpcs:ignore

			if ( ! empty( $so_dismiss_admin_notice ) && '1' === $so_dismiss_admin_notice && ! empty( $so_option_name ) ) {
				if ( strpos( $so_option_name, 'smart_offers_ready_designs_imported' ) !== false ) {
					update_option( $so_option_name, 'no', 'no' );
				} else {
					// always pass option_name below to pass below.
					if ( 'so_bn_merged_notice' === $so_option_name ) {
						// Whether to set option value as yes or no. Pass 'set' in dismiss call to decide.
						if ( 'yes' === $so_option_value ) {
							update_option( $so_option_name . '_smart_offers', 'yes', 'no' );
						} else {
							update_option( $so_option_name . '_smart_offers', 'no', 'no' );
						}
					}
				}
				$referer = wp_get_referer();
				wp_safe_redirect( $referer );
				exit();
			}

		}

		/**
		 * Function to show SA in app offer if any.
		 * Added @since: 3.4.2. Start Date: 26.10.2018
		 */
		public function may_be_show_sa_in_app_offer() {

			global $post;

			$get_post_type = ( ! empty( $_GET['post_type'] ) ) ? wc_clean( wp_unslash( $_GET['post_type'] ) ) : ''; // phpcs:ignore
			$get_page      = ( ! empty( $_GET['page'] ) ) ? wc_clean( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore

			if ( ! class_exists( 'SA_In_App_Offer' ) ) {
				include_once SA_SO_PLUGIN_DIRPATH . '/sa-includes/class-sa-in-app-offer.php';

				$args = array(
					'file'           => SO_PLUGIN_FILE,
					'prefix'         => 'so',
					'option_name'    => 'sa_offer_halloween_2018',
					'campaign'       => 'sa_halloween_2018',
					'start'          => '2018-10-30',
					'end'            => '2018-11-02',
					'is_plugin_page' => ( ( is_object( $post ) && 'smart_offers' === $post->post_type ) ) || ( ( ! empty( $get_post_type ) && ( 'smart_offers' === $get_post_type ) ) || 'sa-wc-buy-now' === $get_page ) ? true : false, // WPCS: CSRF ok.
				);

				SA_In_App_Offer::get_instance( $args );
			}
		}

	}

	return new SO_Admin_Notifications();

}
