<?php // phpcs:ignore WordPress.Files.FileName
/**
 * The class that manange the myaccount endpoints
 *
 * @package YITH\PayPalPayout\Classes
 */

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

if ( ! class_exists( 'YITH_Payout_Endpoints' ) ) {
	/**
	 * Class YITH_Payout_Endpoints
	 */
	class YITH_Payout_Endpoints {
		/**
		 * The query vars
		 *
		 * @var array
		 */
		protected $query_vars = array();

		/**
		 * YITH_Payout_Endpoints constructor.
		 */
		public function __construct() {

			add_action( 'init', array( $this, 'add_woocommerce_query_vars' ), 10 );
			add_action( 'init', array( $this, 'rewrite_rules' ), 20 );
			add_filter( 'woocommerce_account_menu_items', array( $this, 'add_account_menu_items' ), 20 );

			$endpoint_slug = $this->get_payouts_slug();
			add_action( 'woocommerce_account_' . $endpoint_slug . '_endpoint', array( $this, 'show_payouts_content' ) );
			if ( ! is_admin() ) {
				add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0, 1 );
			}
			$this->init_query_vars();

		}


		/**
		 * Add endpoint
		 *
		 * @author YITH <plugins@yithemes.com>
		 * @since 1.0.0
		 */
		public function add_woocommerce_query_vars() {

			foreach ( $this->query_vars as $key => $value ) {

				add_rewrite_endpoint( $value, EP_ROOT | EP_PAGES );
			}
		}

		/**
		 * Flush permalink after add the endpoint
		 *
		 * @since 1.0.0
		 */
		public function rewrite_rules() {

			$rewrite = get_option( 'yith_payouts_rewrite_rule', true );

			if ( $rewrite ) {

				flush_rewrite_rules();
				update_option( 'yith_payouts_rewrite_rule', false );

			}
		}

		/**
		 * Init query vars
		 *
		 * @since 1.0.0
		 */
		public function init_query_vars() {

			$endpoints = apply_filters( 'yith_payouts_get_endpoints', array( 'payouts' => 'payouts' ) );

			foreach ( $endpoints as $key => $endpoint ) {

				$this->query_vars[ $key ] = $endpoint;
			}

		}

		/**
		 * Add End point item in my account menu
		 *
		 * @param array $menu_items The menu items.
		 *
		 * @return array
		 * @since 1.0.0
		 */
		public function add_account_menu_items( $menu_items ) {

			$menu_items['payouts'] = __( 'Payouts', 'yith-paypal-payouts-for-woocommerce' );

			return $menu_items;
		}

		/**
		 * Add query vars
		 *
		 * @param array $vars The query vars.
		 *
		 * @return array
		 * @since 1.0.0
		 */
		public function add_query_vars( $vars ) {
			foreach ( $this->query_vars as $key => $value ) {

				$vars[] = $key;
			}

			return $vars;
		}

		/**
		 * Get the payouts endpoint slug
		 *
		 * @param string $context The context.
		 *
		 * @return string
		 * @since 1.0.0
		 */
		public function get_payouts_slug( $context = 'view' ) {
			if ( 'view' === $context ) {
				return apply_filters( 'yith_get_payouts_slug', 'payouts' );
			} else {
				return 'payouts';
			}
		}

		/**
		 * Show the payouts content
		 *
		 * @param string $value The value.
		 *
		 * @since 1.0.0
		 */
		public function show_payouts_content( $value ) {

			if ( ! is_user_logged_in() ) {
				wp_safe_redirect( esc_url( wc_get_page_permalink( 'myaccount' ) ) );
				exit;
			}

			echo do_shortcode( '[yith_payout_transactions pagination="yes" current_page="' . $value . '"]' );

		}

	}
}

if ( ! function_exists( 'YITH_Payout_Endpoints' ) ) {

	/**
	 * Return the class instance
	 *
	 * @return YITH_Payout_Endpoints
	 */
	function YITH_Payout_Endpoints() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName
		return new YITH_Payout_Endpoints();
	}
}
YITH_Payout_Endpoints();
