<?php

/**
 * Multilingual Compatibility.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */
 
// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * AIOPOD_Public_Multilingual class.
 *
 * @since 1.0.0
 */
class AIOPOD_Public_Multilingual {

	/**
	 * Add custom rewrite rules.
	 *
	 * @since 1.0.0
	 */
	public function init() {
		if ( ! function_exists( 'pll_current_language' ) && ! function_exists( 'pll_get_post_translations' ) ) {
			return;
		}

		$pll_current_language = pll_current_language();
		if ( ! empty( $pll_current_language ) ) {
			return;
		}

		$pages_settings = aiopod_get_option( 'aiopod_pages_settings' );
		$site_url = home_url();
		
		// Single category page		
		if ( ! empty( $pages_settings['category'] ) ) {
			$id = (int) $pages_settings['category'];
			$translations = pll_get_post_translations( $id );

			foreach ( $translations as $lang => $post_id ) {
				$post = get_post( $post_id );

				if ( $post && 'publish' === $post->post_status ) {
					$permalink = get_permalink( $post_id );

					if ( $permalink ) {
						$slug = str_replace( $site_url, '', $permalink );			
						$slug = trim( $slug, '/' );
						$slug = urldecode( $slug );		
						
						add_rewrite_rule( "$slug/([^/]+)/page/?([0-9]{1,})/?$", 'index.php?page_id=' . $post_id . '&aiopod_category=$matches[1]&paged=$matches[2]', 'top' );
						add_rewrite_rule( "$slug/([^/]+)/?$", 'index.php?page_id=' . $post_id . '&aiopod_category=$matches[1]', 'top' );
					}
				}
			}
		}

		// Single tag page
		if ( ! empty( $pages_settings['tag'] ) ) {
			$id = (int) $pages_settings['tag'];
			$translations = pll_get_post_translations( $id );

			foreach ( $translations as $lang => $post_id ) {
				$post = get_post( $post_id );

				if ( $post && 'publish' === $post->post_status ) {
					$permalink = get_permalink( $post_id );

					if ( $permalink ) {
						$slug = str_replace( $site_url, '', $permalink );			
						$slug = trim( $slug, '/' );
						$slug = urldecode( $slug );		
						
						add_rewrite_rule( "$slug/([^/]+)/page/?([0-9]{1,})/?$", 'index.php?page_id=' . $post_id . '&aiopod_tag=$matches[1]&paged=$matches[2]', 'top' );
						add_rewrite_rule( "$slug/([^/]+)/?$", 'index.php?page_id=' . $post_id . '&aiopod_tag=$matches[1]', 'top' );
					}
				}
			}
		}

		// Single participant page
		if ( ! empty( $pages_settings['participant'] ) ) {
			$id = (int) $pages_settings['participant'];
			$translations = pll_get_post_translations( $id );

			foreach ( $translations as $lang => $post_id ) {
				$post = get_post( $post_id );

				if ( $post && 'publish' === $post->post_status ) {
					$permalink = get_permalink( $post_id );

					if ( $permalink ) {
						$slug = str_replace( $site_url, '', $permalink );			
						$slug = trim( $slug, '/' );
						$slug = urldecode( $slug );		
						
						add_rewrite_rule( "$slug/([^/]+)/page/?([0-9]{1,})/?$", 'index.php?page_id=' . $post_id . '&aiopod_participant=$matches[1]&paged=$matches[2]', 'top' );
						add_rewrite_rule( "$slug/([^/]+)/?$", 'index.php?page_id=' . $post_id . '&aiopod_participant=$matches[1]', 'top' );
					}
				}
			}
		}
	}

	/**
	 * [Polylang] Filter the 'aiopod_pages_settings' option.
	 *
	 * @since  1.0.0
	 * @param  array $settings Default settings array.
	 * @return array $settings Filtered array of settings.
	 */
	public function filter_pages_settings_for_polylang( $settings ) {
		if ( ! function_exists( 'pll_current_language' ) && ! function_exists( 'pll_get_post' ) ) {
			return $settings;
		}

		$pll_current_language = pll_current_language();
		if ( empty( $pll_current_language ) ) {
			return $settings;
		}

		foreach ( $settings as $key => $value ) {
			if ( $value > 0 ) {				
				$id = pll_get_post( $value );

				if ( ! empty( $id ) ) {
					$settings[ $key ] = $id;
				}
			}
		}

		return $settings;
	}	
	
}
