<?php
/**
 * Admin notices.
 *
 * @link       https://dcatl.diviaccessories.com/
 * @since      1.0.0
 *
 * @package    display-category-and-taxonomy-list
 * @subpackage display-category-and-taxonomy-list/notice
 */

namespace DCATL\Core;

/**
 * Handles plugin admin notices.
 */
class Notice {

	use \DCATL\Trait\Singleton;

	/**
	 * Notice collection.
	 *
	 * @var \WP_Error
	 */
	private $notices;

	/**
	 * Current notice type.
	 *
	 * @var string
	 */
	private $type = 'success';

	/**
	 * Constructor.
	 */
	public function __construct() {
		$this->notices = new \WP_Error();
	}

	/**
	 * Store a notice.
	 *
	 * @param string $code Notice code.
	 * @param string $message Notice text.
	 * @param string $type Notice type.
	 * @return void
	 */
	public function set_notice( $code, $message, $type = 'success' ) {
		$allowed    = array( 'success', 'info', 'warning', 'error' );
		$this->type = in_array( $type, $allowed, true ) ? $type : 'success';
		$this->notices->add( sanitize_key( $code ), sanitize_text_field( $message ) );
	}

	/**
	 * Get stored notices.
	 *
	 * @return \WP_Error
	 */
	public function get_notices() {
		return $this->notices;
	}

	/**
	 * Clear stored notices.
	 *
	 * @return void
	 */
	public function clear_notices() {
		$this->notices = new \WP_Error();
	}

	/**
	 * Display stored notices.
	 *
	 * @return void
	 */
	public function dcatl_display_notices() {
		foreach ( $this->notices->get_error_messages() as $notice ) {
			printf(
				'<div class="notice notice-%1$s is-dismissible"><p>%2$s</p></div>',
				esc_attr( $this->type ),
				esc_html( $notice )
			);
		}

		$this->clear_notices();
	}

	/**
	 * Display activation and review notices.
	 *
	 * @return void
	 */
	public function dcatl_notice() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( get_transient( 'dcatl_active_notice' ) ) {
			$message = sprintf(
				/* translators: 1: Plugin name, 2: review link. */
				esc_html__( 'Welcome to %1$s. Thank you for installing it. When you have had time to use the plugin, a review at %2$s would be greatly appreciated.', 'display-category-and-taxonomy-list' ),
				'<strong>' . esc_html__( 'Display Category and Taxonomy List', 'display-category-and-taxonomy-list' ) . '</strong>',
				'<a href="' . esc_url( 'https://wordpress.org/plugins/display-category-and-taxonomy-list/#reviews' ) . '" target="_blank" rel="noopener noreferrer">WordPress.org</a>'
			);

			printf(
				'<div class="notice notice-success is-dismissible dcatl-after-install-notice"><p>%s</p></div>',
				wp_kses_post( $message )
			);
			delete_transient( 'dcatl_active_notice' );
		}

		$user_id      = get_current_user_id();
		$install_time = (int) get_user_meta( $user_id, 'dcatl_install_time', true );

		if ( $install_time <= 0 ) {
			update_user_meta( $user_id, 'dcatl_install_time', time() );
			return;
		}

		if ( ( time() - $install_time ) < WEEK_IN_SECONDS ) {
			return;
		}

		$plugin_url = 'https://wordpress.org/plugins/display-category-and-taxonomy-list/';
		$review_url = $plugin_url . '#reviews';
		?>
		<div class="notice notice-info dcatl-notice-review is-dismissible">
			<p>
				<?php
				printf(
					/* translators: %s: Plugin name. */
					esc_html__( 'You have been using %s for a while. If it has been useful, please consider leaving a five-star review.', 'display-category-and-taxonomy-list' ),
					'<strong><a href="' . esc_url( $plugin_url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Display Category and Taxonomy List', 'display-category-and-taxonomy-list' ) . '</a></strong>'
				);
				?>
			</p>
			<p>
				<a href="<?php echo esc_url( $review_url ); ?>" class="button button-primary dcatl-review-write-btn" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Leave a Review', 'display-category-and-taxonomy-list' ); ?></a>
				<a href="#" class="button dcatl-review-done-btn"><?php esc_html_e( 'I Already Reviewed', 'display-category-and-taxonomy-list' ); ?></a>
				<a href="#" class="button dcatl-review-dismiss-btn"><?php esc_html_e( 'Maybe Later', 'display-category-and-taxonomy-list' ); ?></a>
			</p>
		</div>
		<?php
	}

	/**
	 * Reschedule or dismiss the review notice.
	 *
	 * @return void
	 */
	public function dcatl_review_dismiss_action() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'You are not allowed to perform this action.', 'display-category-and-taxonomy-list' ) ), 403 );
		}

		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'dcatl_nonce' ) ) {
			wp_send_json_error( array( 'message' => esc_html__( 'Nonce verification failed.', 'display-category-and-taxonomy-list' ) ), 403 );
		}

		$delay    = isset( $_POST['delay'] ) ? sanitize_key( wp_unslash( $_POST['delay'] ) ) : 'year';
		$interval = 'month' === $delay ? MONTH_IN_SECONDS : YEAR_IN_SECONDS;
		$base     = max( time(), time() + $interval - WEEK_IN_SECONDS );

		update_user_meta( get_current_user_id(), 'dcatl_install_time', $base );
		wp_send_json_success();
	}

	/**
	 * Add custom footer text only on the plugin page.
	 *
	 * @return void
	 */
	public function check_dcatl_page() {
		$screen = get_current_screen();
		if ( $screen && 'toplevel_page_display-category-and-taxonomy-list' === $screen->id ) {
			add_filter( 'admin_footer_text', array( $this, 'dcatl_footer_notice' ), 99 );
		}
	}

	/**
	 * Return plugin-page footer text.
	 *
	 * @return string
	 */
	public function dcatl_footer_notice() {
		$message = sprintf(
			/* translators: 1: Plugin name, 2: review URL, 3: support URL. */
			__( 'Enjoying <strong>%1$s</strong>? <a href="%2$s" target="_blank" rel="noopener noreferrer">Leave a five-star review</a> or share a suggestion through the <a href="%3$s" target="_blank" rel="noopener noreferrer">support page</a>.', 'display-category-and-taxonomy-list' ),
			esc_html__( 'Display Category and Taxonomy List', 'display-category-and-taxonomy-list' ),
			esc_url( 'https://wordpress.org/plugins/display-category-and-taxonomy-list/#reviews' ),
			esc_url( 'https://dcatl.diviaccessories.com/' )
		);

		return wp_kses_post( $message );
	}
}
