<?php

/**
 * Helper functions for generating and managing custom URLs and permalinks.
 *
 * @link    https://aiopodcasting.com
 * @since   1.0.0
 *
 * @package AiO_Podcast_Studio
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Get the category page URL.
 *
 * @since  1.0.0
 * @param  object $term The term object.
 * @return string       Category page URL.
 */
function aiopod_get_category_page_url( $term ) {
	$pages_settings = aiopod_get_option( 'aiopod_pages_settings' );
	
	$url = '/';
	
	if ( $pages_settings['category'] > 0 ) {
		$url = get_permalink( $pages_settings['category'] );
	
		if ( '' !== get_option( 'permalink_structure' ) ) {
    		$url = user_trailingslashit( trailingslashit( $url ) . $term->slug );
  		} else {
    		$url = add_query_arg( 'aiopod_category', $term->slug, $url );
  		}
	}
  
	return apply_filters( 'aiopod_category_page_url', $url, $term );
}

/**
 * Get the tracking (download) URL for an episode file.
 *
 * The pretty-permalink URL uses the format:
 *   /{episode-base}/{episode-slug}/file/episode.{ext}
 * where {ext} is derived from the actual episode media file URL.
 *
 * @since  1.0.0
 * @param  WP_Post $post Episode post object.
 * @return string        Tracking URL for the episode file.
 */
function aiopod_get_episode_file_url( $post ) {
	if ( '' !== get_option( 'permalink_structure' ) ) {
		$permalink = untrailingslashit( get_permalink( $post ) );

		// Derive the file extension from the stored episode_file meta.
		$file_url = get_post_meta( $post->ID, 'episode_file', true );
		$ext      = '';

		if ( ! empty( $file_url ) ) {
			$parsed = parse_url( $file_url );
			$path   = isset( $parsed['path'] ) ? $parsed['path'] : '';
			$ext    = strtolower( pathinfo( $path, PATHINFO_EXTENSION ) );

			// Accept only safe, alphanumeric extensions.
			if ( ! preg_match( '/^[a-z0-9]{1,10}$/', $ext ) ) {
				$ext = '';
			}
		}

		if ( ! empty( $ext ) ) {
			$url = $permalink . '/file/episode.' . $ext;
		} else {
			$url = $permalink . '/file/';
		}
	} else {
		$url = add_query_arg(
			array(
				'aiopod_episode'  => $post->post_name,
				'aiopod_download' => '1',
			),
			home_url( '/' )
		);
	}

	return apply_filters( 'aiopod_episode_file_url', $url, $post );
}

/**
 * Get the participant page URL.
 *
 * @since  1.0.0
 * @param  object $term The term object.
 * @return string       Participant page URL.
 */
function aiopod_get_participant_page_url( $term ) {
	$pages_settings = aiopod_get_option( 'aiopod_pages_settings' );
	
	$url = '/';
	
	if ( $pages_settings['participant'] > 0 ) {
		$url = get_permalink( $pages_settings['participant'] );
	
		if ( '' !== get_option( 'permalink_structure' ) ) {
    		$url = user_trailingslashit( trailingslashit( $url ) . $term->slug );
  		} else {
    		$url = add_query_arg( 'aiopod_participant', $term->slug, $url );
  		}
	}
  
	return apply_filters( 'aiopod_participant_page_url', $url, $term );
}

/**
 * Get the RSS feed URL for a podcast.
 *
 * @since  1.0.0
 * @param  WP_Post $post Podcast post object.
 * @return string       RSS feed URL.
 */
function aiopod_get_rss_feed_url( $post ) {
	// Bail out early when post_name is empty (post not yet saved).
	if ( empty( $post->post_name ) ) {
		return apply_filters( 'aiopod_rss_feed_url', '', $post );
	}

	if ( '' !== get_option( 'permalink_structure' ) ) {
		$permalink = untrailingslashit( get_permalink( $post ) );
		$url       = $permalink . '/feed/';
	} else {
		$url = add_query_arg( array(
			'aiopod_podcast' => $post->post_name,
			'aiopod_feed'    => 'podcast'
		), home_url( '/' ) );
	}

	return apply_filters( 'aiopod_rss_feed_url', $url, $post );
}

/**
 * Get the tag page URL.
 *
 * @since  1.0.0
 * @param  object $term The term object.
 * @return string       Tag page URL.
 */
function aiopod_get_tag_page_url( $term ) {
	$pages_settings = aiopod_get_option( 'aiopod_pages_settings' );
	
	$url = '/';
	
	if ( $pages_settings['tag'] > 0 ) {
		$url = get_permalink( $pages_settings['tag'] );
	
		if ( '' !== get_option( 'permalink_structure' ) ) {
    		$url = user_trailingslashit( trailingslashit( $url ) . $term->slug );
  		} else {
    		$url = add_query_arg( 'aiopod_tag', $term->slug, $url );
  		}
	}
  
	return apply_filters( 'aiopod_tag_page_url', $url, $term );
}

