<?php
/**
 * This file has all plugins functions
 *
 * @package YITH\PayPalPayouts\Functions
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'yith_payouts_add_message' ) ) {
	/**
	 * Add the message in the transient
	 *
	 * @param string $message The message.
	 *
	 * @since 2.0.0
	 * @author YITH <plugins@yithemes.com>
	 */
	function yith_payouts_add_message( $message ) {
		$payouts_message = get_transient( 'yith_payouts_messages' );
		if ( isset( $payouts_message['success'] ) ) {
			$payouts_message['success'][] = $message;
		} else {
			$payouts_message['success'] = array( $message );
		}
		set_transient( 'yith_payouts_messages', $payouts_message );
	}
}

if ( ! function_exists( 'yith_payouts_add_error' ) ) {
	/**
	 * Add the error message
	 *
	 * @param string $message The message to add.
	 *
	 * @since 2.0.0
	 */
	function yith_payouts_add_error( $message ) {
		$payouts_message = get_transient( 'yith_payouts_messages' );
		if ( isset( $payouts_message['error'] ) ) {
			$payouts_message['error'] [] = $message;
		} else {
			$payouts_message = array(
				'error' => array( $message ),
			);
		}
		set_transient( 'yith_payouts_messages', $payouts_message );
	}
}
if ( ! function_exists( 'yith_payouts_show_messages' ) ) {
	/**
	 * Show the message in admin
	 *
	 * @since 2.0.0
	 */
	function yith_payouts_show_messages() {
		$payouts_message = get_transient( 'yith_payouts_messages' );
		if ( is_array( $payouts_message ) && isset( $payouts_message['success'] ) ) {
			$success_messages = $payouts_message['success'];
			$success_messages = implode( '<br/>', $success_messages );
			?>
			<div id="yith-payouts-success-message" class="notice-success is-dismissible">
				<p><?php echo wp_kses_post( $success_messages ); ?></p>
				<button type="button" class="notice-dismiss">
				<span
					class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'yith-paypal-payouts-for-woocommerce' ); ?>
				</span>
				</button>
			</div>
			<?php
		}

		if ( is_array( $payouts_message ) && isset( $payouts_message['error'] ) ) {
			$error_messages = $payouts_message['error'];
			$error_messages = implode( '<br/>', $error_messages );
			?>
			<div id="yith-payouts-error-message" class="notice-error is-dismissible">
				<p><?php echo wp_kses_post( $error_messages ); ?></p>
				<button type="button" class="notice-dismiss">
				<span
					class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice.', 'yith-paypal-payouts-for-woocommerce' ); ?>
				</span>
				</button>
			</div>
			<?php
		}
		delete_transient( 'yith_payouts_messages' );
	}
}
