<?php

/**
 * The plugin bootstrap file
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              sonaar.io
 * @since             1.0.0
 * @package           Sonaar_Music
 *
 * @wordpress-plugin
 * Plugin Name:       MP3 Audio Player by Sonaar
 * Plugin URI:        https://sonaar.io/mp3-audio-player-pro/?utm_source=Sonaar+Music+Free+Plugin&utm_medium=plugin
 * Description:       The most popular and complete Music & Podcast Player for WordPress.
 * Version:           5.11
 * Author:            Sonaar Music
 * Author URI:        https://sonaar.io/?utm_source=Sonaar%20Music%20Free%20Plugin&utm_medium=plugin
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       sonaar-music
 * Domain Path:       /languages
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

define('SRMP3_VERSION', '5.11'); // important to avoid cache issues on update
define('SRMP3_PRO_MIN_VERSION', '5.11'); // Minimum pro version required
if ( !defined( 'SRMP3_DIR_PATH' ) ) {
    define( 'SRMP3_DIR_PATH', plugin_dir_path( __FILE__ ) );
}
if ( !class_exists( 'Sonaar_Music' )) {

	/**
	 * The core plugin class that is used to define internationalization,
	 * admin-specific hooks, and public-facing site hooks.
	 */
	require plugin_dir_path( __FILE__ ) . 'includes/class-sonaar-music.php';
	
	register_activation_hook(__FILE__,  'srmp3_activate');
	
	function srmp3_activate() {
        add_option('srmp3_free_wizard_redirect', true);
    }
	/**
	 * Begins execution of the plugin.
	 *
	 * Since everything within the plugin is registered via hooks,
	 * then kicking off the plugin from this point in the file does
	 * not affect the page life cycle.
	 *
	 * @since    1.0.0
	 */


	function srmp3_set_template( $template ){
		//Add option for plugin to turn this off? If so just return $template
		//Check if the taxonomy/single is being viewed 
		if( is_archive() && is_tax('podcast-show') || is_archive() && is_tax('playlist-category'))
			return srmp3_template_path('taxonomy-show');

		if ( is_single() && SR_PLAYLIST_CPT === get_queried_object()->post_type)
			return srmp3_template_path('single-album');

		return $template;
	}

	function srmp3_template_path( $fileName ){
		if(file_exists(get_stylesheet_directory().'/mp3-music-player-by-sonaar/'.$fileName.'.php')){
			return get_stylesheet_directory().'/mp3-music-player-by-sonaar/'.$fileName.'.php';
		}
		return dirname( __FILE__ ) . '/templates/'.$fileName.'.php';
	}

	
	

	function srmp3_register_elementor_locations( $elementor_theme_manager ) {
		$elementor_theme_manager->register_location( 'playlist' );
	}

	function srmp3_create_customfeed() {
		load_template( plugin_dir_path( __FILE__ ) .'templates/podcast-feed.php');
	}
	
	function srmp3_custom_feed_rewrite($wp_rewrite) {
		$feed_rules = array(
			'feed/(.+)' => 'index.php?feed=' . $wp_rewrite->preg_index(1),
		);
		$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
	}

	function srmp3_feed_content_type( $content_type = '', $type = '' ) {
		if ( apply_filters( 'sonaar_feed_slug', 'podcast' ) === $type ) {
			$content_type = 'text/xml';
		}
		return $content_type;
	}

	if ( Sonaar_Music::get_option('player_type', 'srmp3_settings_general') == 'podcast' ){
		$sr_disable_rss = (Sonaar_Music::get_option('podcast_setting_rssfeed_disable', 'srmp3_settings_general') === "true") ? true : false;
		if( !$sr_disable_rss ){
			if( Sonaar_Music::get_option('podcast_setting_rssfeed_redirect', 'srmp3_settings_general') === "true" && Sonaar_Music::get_option('podcast_setting_rssfeed_slug', 'srmp3_settings_general') != ''){
				$podcast_feed_slug = Sonaar_Music::get_option('podcast_setting_rssfeed_slug', 'srmp3_settings_general');
			}else{
				$podcast_feed_slug = 'podcast'; //default
			}
			add_action( 'do_feed_'.$podcast_feed_slug, 'srmp3_create_customfeed', 10, 1); //do_feed_{$feed} will set {$feed} as /feed/{$feed} url
			add_filter( 'generate_rewrite_rules',  'srmp3_custom_feed_rewrite');
			add_filter( 'feed_content_type',  'srmp3_feed_content_type', 10, 2 );
		}
	}
	
	add_filter( 'template_include', 'srmp3_set_template');
	add_action( 'elementor/theme/register_locations', 'srmp3_register_elementor_locations' );

	
	function run_sonaar_music() {
		$plugin = new Sonaar_Music();
		$plugin->run();
	}
	
	run_sonaar_music();

}

add_action('wp_ajax_import_srmp3_elementor_template', 'import_srmp3_elementor_template');
add_action('wp_ajax_load_post_by_ajax', 'load_post_by_ajax_callback');
add_action('wp_ajax_nopriv_load_post_by_ajax', 'load_post_by_ajax_callback');
 
function load_post_by_ajax_callback() {
    check_ajax_referer('sonaar_music_ajax_nonce', 'nonce');
	$stringValue = sanitize_text_field($_POST['store-id']);

	/*Validation*/
    $validatedCharacters = ['a','0','1','2','3','4','5','6','7','8','9','-'];
	$validation = true;
	for ($i=0; $i<strlen($stringValue); $i++) {
		if( ! in_array($stringValue[$i], $validatedCharacters)){
			$validation = false;
		}
	}
	if(!$validation){
		wp_die();
	}

	$storeID = explode('-', $stringValue); 

	/* $storeID = array[track number, store number]
	$storeID[track number] == "a" when store is from the "avalable now" section
	*/
	if( $storeID[0] == 'a' ){   //store from the "available now" section
		$response = get_post_meta(sanitize_text_field($_POST['id']), 'alb_store_list', true );
		echo  json_encode(do_shortcode(nl2br($response[$storeID[1]]['store-content'])));
	}else{ //store from the tracklist
		$response = get_post_meta(sanitize_text_field($_POST['id']), 'alb_tracklist', true );
		$response = json_encode(do_shortcode(nl2br( $response[$storeID[0]]['song_store_list'][$storeID[1]]['store-content'])));
		if (strpos($response, '\/wp-admin\/admin-ajax.php')) { //Replace the right post url
			$postPermalink = nl2br(str_replace(home_url(), '', get_permalink($_POST['id']))); 	
			$response = str_replace('\/wp-admin\/admin-ajax.php', $postPermalink, $response); 
		}
		
	
		echo   $response   ;
	}
	 wp_die();
}


add_action('wp_ajax_load_track_note_ajax', 'load_track_note_ajax_callback');
add_action('wp_ajax_nopriv_load_track_note_ajax', 'load_track_note_ajax_callback'); 

function load_track_note_ajax_callback() {
	check_ajax_referer('sonaar_music_ajax_nonce', 'nonce');

	$post_id = absint($_POST['post-id']);
	if (!$post_id) {
		wp_send_json_error('Invalid post ID');
	}

	if (!empty($_POST['track-desc-postcontent']) && $_POST['track-desc-postcontent'] == '1') {

		$postobj = get_post($post_id);
		if (!$postobj) {
			wp_send_json_error('Post not found');
		}

		if ($postobj->post_status !== 'publish') {
			if (!is_user_logged_in() || !current_user_can('read_post', $post_id)) {
				wp_send_json_error('Permission denied');
			}
		}

		$description = wp_kses_post($postobj->post_content);

	} else {

		$tracks = get_post_meta($post_id, 'alb_tracklist', true);
		$track_position = absint($_POST['track-position']);

		if (!isset($tracks[$track_position]['track_description'])) {
			wp_send_json_error('Track not found');
		}

		$description = wp_kses_post($tracks[$track_position]['track_description']);
	}

	echo wp_json_encode(
		'<div class="srp_note_title">' .
		sanitize_text_field(stripslashes($_POST['track-title'])) .
		'</div>' .
		$description
	);

	wp_die();
}

add_action('wp_ajax_load_lyrics_ajax', 'load_lyrics_ajax_callback');

function load_lyrics_ajax_callback() {
    check_ajax_referer('sonaar_music_ajax_nonce', 'nonce');

    if (!current_user_can('manage_options')) {
        wp_send_json_error('Permission denied');
    }

    $post_id = absint($_POST['post-id']);
    if (!$post_id) {
        wp_send_json_error('Invalid post ID');
    }

    $track_position = isset($_POST['track-position']) ? absint($_POST['track-position']) : null;

    $ttml_content = get_post_meta($post_id, 'sr_sonaar_tts_post_ttml', true);
    $postmeta = get_post_meta($post_id, 'alb_tracklist', true);

    if (
        $ttml_content ||
        ($track_position !== null && isset($postmeta[$track_position]['track_lyrics']))
    ) {
        $ttml_content = $ttml_content ?: $postmeta[$track_position]['track_lyrics'];

        if (!filter_var($ttml_content, FILTER_VALIDATE_URL)) {
            wp_send_json_error('Invalid URL');
        }

        $response = wp_safe_remote_get($ttml_content, array(
            'timeout' => 5,
        ));

        if (is_wp_error($response)) {
            wp_send_json_error($response->get_error_message());
        }

        echo wp_json_encode(wp_remote_retrieve_body($response));
    } else {
        wp_send_json_error('Lyrics not found');
    }

    wp_die();
}


