<?php
/**
 * Admin: Dashboard page.
 *
 * @package The Studio Player
 */

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

/**
 * Build dashboard license state with safe defaults.
 *
 * @param bool $has_pro_assets Whether Pro assets are active.
 * @return array
 */
function thespl_get_dashboard_license_state( $has_pro_assets ) {
	$state = array(
		'license_status'      => array(),
		'license_state'       => 'free',
		'plan_kind'           => 'free',
		'support_state'       => 'unknown',
		'account_issue'       => false,
		'license_label'       => __( 'Free', 'the-studio-player' ),
		'license_plan_label'  => __( 'Free Edition', 'the-studio-player' ),
		'license_extra_line'  => '',
		'license_extra_label' => '',
		'has_intro_offer'     => false,
		'support_status_text' => '',
		'support_tone_class'  => '',
		'is_lifetime'         => false,
		'is_pro_like'         => false,
	);

	return apply_filters( 'thespl_dashboard_license_state', $state, (bool) $has_pro_assets );
}

/**
 * Render Dashboard page.
 */
function thespl_render_dashboard_page() {
	if ( ! thespl_current_user_can_policy( 'plugin_access' ) ) {
		return;
	}

	global $wpdb;

	$has_opt_in_post = isset( $_POST['thespl_remote_opt_in_choice'] ) || isset( $_POST['thespl_uninstall_remove_uploads_choice'] );
	if ( $has_opt_in_post && thespl_current_user_can_policy( 'settings_general' ) && isset( $_POST['thespl_remote_opt_in_nonce'] ) ) {
		check_admin_referer( 'thespl_remote_opt_in_choice', 'thespl_remote_opt_in_nonce' );
		$choice_in = null;
		if ( isset( $_POST['thespl_remote_opt_in_choice'] ) ) {
			$choice_in = sanitize_text_field( wp_unslash( $_POST['thespl_remote_opt_in_choice'] ) );
		}
		if ( is_string( $choice_in ) ) {
			$choice = strtolower( trim( $choice_in ) );
			if ( 'yes' === $choice || 'no' === $choice ) {
				update_option( 'thespl_remote_content_opt_in', $choice );
			}
		}
		$uninstall_choice_in = null;
		if ( isset( $_POST['thespl_uninstall_remove_uploads_choice'] ) ) {
			$uninstall_choice_in = sanitize_text_field( wp_unslash( $_POST['thespl_uninstall_remove_uploads_choice'] ) );
		}
		if ( is_string( $uninstall_choice_in ) ) {
			$uninstall_choice = strtolower( trim( $uninstall_choice_in ) );
			if ( 'yes' === $uninstall_choice || 'no' === $uninstall_choice ) {
				update_option( 'thespl_uninstall_remove_uploads', $uninstall_choice );
			}
		}
	}

	$ver             = thespl_get_readme_stable_tag_version();
	$plugin_name     = __( 'The Studio Player', 'the-studio-player' );
	$page_label      = __( 'Dashboard', 'the-studio-player' );
	$page_title      = $plugin_name;
	$profile         = thespl_get_capability_profile();
	$has_pro_build   = isset( $profile['is_pro'] ) ? (bool) $profile['is_pro'] : false;
	$has_pro_assets  = function_exists( 'thespl_has_pro_assets' ) ? (bool) thespl_has_pro_assets() : false;
	$display_version = $ver;
	if ( '' !== $display_version && $has_pro_build ) {
		$display_version .= ' Pro';
	}
	$license_runtime     = thespl_get_dashboard_license_state( $has_pro_assets );
	$license_status      = isset( $license_runtime['license_status'] ) && is_array( $license_runtime['license_status'] ) ? $license_runtime['license_status'] : array();
	$license_state       = isset( $license_runtime['license_state'] ) ? (string) $license_runtime['license_state'] : 'free';
	$plan_kind           = isset( $license_runtime['plan_kind'] ) ? (string) $license_runtime['plan_kind'] : 'free';
	$support_state       = isset( $license_runtime['support_state'] ) ? (string) $license_runtime['support_state'] : 'unknown';
	$account_issue       = isset( $license_runtime['account_issue'] ) ? (bool) $license_runtime['account_issue'] : false;
	$license_label       = isset( $license_runtime['license_label'] ) ? (string) $license_runtime['license_label'] : __( 'Free', 'the-studio-player' );
	$license_plan_label  = isset( $license_runtime['license_plan_label'] ) ? (string) $license_runtime['license_plan_label'] : __( 'Free Edition', 'the-studio-player' );
	$license_extra_line  = isset( $license_runtime['license_extra_line'] ) ? (string) $license_runtime['license_extra_line'] : '';
	$license_extra_label = isset( $license_runtime['license_extra_label'] ) ? (string) $license_runtime['license_extra_label'] : '';
	$has_intro_offer     = ! empty( $license_runtime['has_intro_offer'] );
	$support_status_text = isset( $license_runtime['support_status_text'] ) ? (string) $license_runtime['support_status_text'] : '';
	$support_tone_class  = isset( $license_runtime['support_tone_class'] ) ? (string) $license_runtime['support_tone_class'] : '';
	$is_lifetime         = ! empty( $license_runtime['is_lifetime'] );
	$is_pro_like         = ! empty( $license_runtime['is_pro_like'] );
	if ( '' !== $ver ) {
		$page_title .= ' - ' . $ver;
		if ( $has_pro_build ) {
			$page_title .= ' Pro';
		}
	}
	$page_title        .= ' - ' . $page_label;
	$post_max_size      = ini_get( 'post_max_size' );
	$upload_max_size    = ini_get( 'upload_max_filesize' );
	$wp_upload_bytes    = wp_max_upload_size();
	$wp_upload_human    = size_format( $wp_upload_bytes );
	$memory_limit       = ini_get( 'memory_limit' );
	$max_execution_time = ini_get( 'max_execution_time' );
	$site_url           = get_site_url();
	$home_url           = get_home_url();
	$is_multisite       = is_multisite() ? 'yes' : 'no';
	$theme              = wp_get_theme();
	$theme_name         = '';
	$theme_version      = '';
	if ( $theme ) {
		$theme_name    = $theme->get( 'Name' );
		$theme_version = $theme->get( 'Version' );
	}
	$theme_display = 'n/a';
	if ( '' !== $theme_name ) {
		$theme_display = $theme_name;
		if ( '' !== $theme_version ) {
			$theme_display .= ' (v' . $theme_version . ')';
		}
	}
	$parent_display = 'none';
	if ( $theme && $theme->parent() ) {
		$parent       = $theme->parent();
		$parent_name  = $parent->get( 'Name' );
		$parent_ver   = $parent->get( 'Version' );
		$parent_label = '';
		if ( '' !== $parent_name ) {
			$parent_label = $parent_name;
			if ( '' !== $parent_ver ) {
				$parent_label .= ' (v' . $parent_ver . ')';
			}
		}
		if ( '' !== $parent_label ) {
			$parent_display = $parent_label;
		}
	}
	$db_version = 'n/a';
	if ( isset( $wpdb ) && is_object( $wpdb ) && method_exists( $wpdb, 'db_version' ) ) {
		$db_version = $wpdb->db_version();
	}
	$wp_debug       = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? 'true' : 'false';
	$wp_debug_log   = ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) ? 'true' : 'false';
	$wp_cache       = ( defined( 'WP_CACHE' ) && WP_CACHE ) ? 'true' : 'false';
	$script_debug   = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? 'true' : 'false';
	$php_sapi       = PHP_SAPI;
	$max_input_vars = ini_get( 'max_input_vars' );
	$max_input_time = ini_get( 'max_input_time' );
	$curl_info      = 'not available';
	if ( function_exists( 'curl_version' ) ) {
		$curl_data    = curl_version();
		$curl_version = isset( $curl_data['version'] ) ? $curl_data['version'] : '';
		$curl_ssl     = isset( $curl_data['ssl_version'] ) ? $curl_data['ssl_version'] : '';
		if ( '' !== $curl_version && '' !== $curl_ssl ) {
			$curl_info = $curl_version . ' (SSL: ' . $curl_ssl . ')';
		} elseif ( '' !== $curl_version ) {
			$curl_info = $curl_version;
		}
	}
	$ext_flags = array();
	foreach ( array( 'curl', 'mbstring', 'json', 'openssl' ) as $ext_name ) {
		$ext_flags[] = $ext_name . '=' . ( extension_loaded( $ext_name ) ? 'enabled' : 'disabled' );
	}
	$extensions_summary = implode( ', ', $ext_flags );
	$system_lines       = array(
		'==== THE STUDIO PLAYER – SUPPORT-DUMP ====',
		'Generated: ' . gmdate( 'Y-m-d H:i:s' ) . ' UTC',
		'',
		'Plugin Version: ' . $display_version,
		'Edition: ' . $license_label,
		'Plan: ' . $license_plan_label,
		'PHP Version: ' . PHP_VERSION,
		'WordPress Version: ' . get_bloginfo( 'version' ),
		'Memory Limit: ' . $memory_limit,
		'Max Execution Time: ' . $max_execution_time,
		'Max POST Size: ' . $post_max_size,
		'Upload Max Filesize: ' . $upload_max_size,
		'WP Max Upload Size: ' . $wp_upload_human,
		'Site URL: ' . $site_url,
		'Home URL: ' . $home_url,
		'Multisite: ' . $is_multisite,
		'Active Theme: ' . $theme_display,
		'Parent Theme: ' . $parent_display,
		'DB Version: ' . $db_version,
		'WP_DEBUG: ' . $wp_debug,
		'WP_DEBUG_LOG: ' . $wp_debug_log,
		'WP_CACHE: ' . $wp_cache,
		'SCRIPT_DEBUG: ' . $script_debug,
		'PHP SAPI: ' . $php_sapi,
		'max_input_vars: ' . $max_input_vars,
		'max_input_time: ' . $max_input_time,
		'cURL: ' . $curl_info,
		'Extensions: ' . $extensions_summary,
	);
	if ( $has_intro_offer && '' !== $license_extra_line && '' !== $license_extra_label ) {
		$system_lines[] = $license_extra_label . ': ' . $license_extra_line;
	}

	$player_lines = array(
		'',
		'==== THE STUDIO PLAYER – GLOBALE TECHNISCHE EINSTELLUNGEN ====',
		'[thespl.player_settings]',
	);

	$player_settings = thespl_get_player_settings();

	$player_lines[] = 'debug_console_enabled = ' . ( ! empty( $player_settings['debug_console_enabled'] ) ? 'true' : 'false' );
	$player_lines[] = 'streaming_auto_detect_network = ' . ( ! empty( $player_settings['streaming_auto_detect_network'] ) ? 'true' : 'false' );

	$chunk_level = isset( $player_settings['streaming_chunk_level'] ) ? (int) $player_settings['streaming_chunk_level'] : 4;
	if ( $chunk_level < 0 ) {
		$chunk_level = 0;
	}
	if ( $chunk_level > 9 ) {
		$chunk_level = 9;
	}
	$player_lines[] = 'streaming_chunk_level = ' . $chunk_level;

	$prebuffer_sec = isset( $player_settings['streaming_prebuffer_sec'] ) ? (int) $player_settings['streaming_prebuffer_sec'] : 0;
	if ( $prebuffer_sec < 0 ) {
		$prebuffer_sec = 0;
	}
	if ( $prebuffer_sec > 30 ) {
		$prebuffer_sec = 30;
	}
	$player_lines[] = 'streaming_prebuffer_sec = ' . $prebuffer_sec;

	$player_lines[] = 'streaming_continue_buffer_on_pause = ' . ( ! empty( $player_settings['streaming_continue_buffer_on_pause'] ) ? 'true' : 'false' );
	$player_lines[] = 'streaming_prefetch_next_on_ahead = ' . ( ! empty( $player_settings['streaming_prefetch_next_on_ahead'] ) ? 'true' : 'false' );

	$ready_ahead_sec = isset( $player_settings['streaming_ready_ahead_sec'] ) ? (int) $player_settings['streaming_ready_ahead_sec'] : 1;
	if ( $ready_ahead_sec < 1 ) {
		$ready_ahead_sec = 1;
	}
	if ( $ready_ahead_sec > 10 ) {
		$ready_ahead_sec = 10;
	}
	$player_lines[] = 'streaming_ready_ahead_sec = ' . $ready_ahead_sec;

	$fade_ms = isset( $player_settings['fade_switch_duration_ms'] ) ? (int) $player_settings['fade_switch_duration_ms'] : 1;
	if ( $fade_ms < 0 ) {
		$fade_ms = 0;
	}
	if ( $fade_ms > 50 ) {
		$fade_ms = 50;
	}
	$player_lines[] = 'fade_switch_duration_ms = ' . $fade_ms;

	$player_lines[] = 'streaming_security_enabled = ' . ( ! empty( $player_settings['streaming_security_enabled'] ) ? 'true' : 'false' );
	$player_lines[] = 'streaming_htaccess_enabled = ' . ( ! empty( $player_settings['streaming_htaccess_enabled'] ) ? 'true' : 'false' );

	$token_lifetime = isset( $player_settings['streaming_token_lifetime'] ) ? (int) $player_settings['streaming_token_lifetime'] : 60;
	if ( $token_lifetime < 1 ) {
		$token_lifetime = 1;
	}
	if ( $token_lifetime > 1440 ) {
		$token_lifetime = 1440;
	}
	$player_lines[] = 'streaming_token_lifetime = ' . $token_lifetime;

	$player_lines[] = 'streaming_rate_limit_enabled = ' . ( ! empty( $player_settings['streaming_rate_limit_enabled'] ) ? 'true' : 'false' );

	$rpm = isset( $player_settings['streaming_rate_limit_rpm'] ) ? (int) $player_settings['streaming_rate_limit_rpm'] : 300;
	if ( $rpm < 10 ) {
		$rpm = 10;
	}
	if ( $rpm > 1000 ) {
		$rpm = 1000;
	}
	$player_lines[] = 'streaming_rate_limit_rpm = ' . $rpm;

	$burst = isset( $player_settings['streaming_rate_limit_burst'] ) ? (int) $player_settings['streaming_rate_limit_burst'] : 80;
	if ( $burst < 5 ) {
		$burst = 5;
	}
	if ( $burst > 500 ) {
		$burst = 500;
	}
	$player_lines[] = 'streaming_rate_limit_burst = ' . $burst;

	$min_capability = isset( $player_settings['min_capability'] ) && is_string( $player_settings['min_capability'] ) ? $player_settings['min_capability'] : 'manage_options';
	$player_lines[] = 'min_capability = "' . $min_capability . '"';

	$player_lines[] = '';
	$player_lines[] = '[thespl.muxer_settings]';

	$muxer_settings = thespl_get_muxer_settings();
	$subdir         = isset( $muxer_settings['subdir'] ) && is_string( $muxer_settings['subdir'] ) ? $muxer_settings['subdir'] : '';
	$chunk_size     = isset( $muxer_settings['chunk_size'] ) ? (int) $muxer_settings['chunk_size'] : 0;

	$player_lines[] = 'subdir = "' . $subdir . '"';
	$player_lines[] = 'chunk_size = ' . $chunk_size;

	$player_lines[] = '';
	$player_lines[] = '[thespl.streaming_secret]';

	$secret       = get_option( 'thespl_streaming_secret_key', '' );
	$secret_str   = is_string( $secret ) ? $secret : '';
	$secret_len   = strlen( $secret_str );
	$secret_valid = $secret_len > 0;

	$player_lines[] = 'present = ' . ( $secret_valid ? 'true' : 'false' );
	$player_lines[] = 'length = ' . $secret_len;

	$player_lines[] = '';
	$player_lines[] = '==== THE STUDIO PLAYER – TEMPLATE-REGISTRY & OVERRIDES ====';
	$player_lines[] = '[thespl.templates]';

	$template_sources = array();

	$templates = thespl_get_available_player_templates();
	if ( is_array( $templates ) ) {
		foreach ( $templates as $slug => $label ) {
			if ( ! is_string( $slug ) || '' === $slug ) {
				continue;
			}
			$slug_key = sanitize_key( $slug );
			if ( '' === $slug_key ) {
				continue;
			}

			$src_list      = array();
			$uploads       = wp_upload_dir();
			$has_upload    = false;
			$has_plugin    = false;
			$uploads_based = '';

			if ( ! empty( $uploads['basedir'] ) && is_string( $uploads['basedir'] ) ) {
				$uploads_based = trailingslashit( $uploads['basedir'] ) . 'the-studio-player/templates/player/';
				$uploads_file  = $uploads_based . $slug_key . '/player.php';
				if ( file_exists( $uploads_file ) ) {
					$has_upload = true;
				}
			}

			$plugin_base = trailingslashit( THESPL_PLUGIN_DIR . 'templates/player' );
			$plugin_file = $plugin_base . $slug_key . '/player.php';
			if ( file_exists( $plugin_file ) ) {
				$has_plugin = true;
			}

			if ( $has_plugin ) {
				$src_list[] = 'plugin';
			}
			if ( $has_upload ) {
				$src_list[] = 'uploads';
			}

			if ( empty( $src_list ) ) {
				continue;
			}

			$src_label                     = implode( '+', $src_list );
			$template_sources[ $slug_key ] = array(
				'source' => $src_label,
			);

			$allows_overrides = thespl_template_allows_user_css_overrides( $slug_key );

			$player_lines[] = 'templates.' . $slug_key . '.source = "' . $src_label . '"';
			$player_lines[] = 'templates.' . $slug_key . '.allows_user_css_overrides = ' . ( $allows_overrides ? 'true' : 'false' );
			$player_lines[] = '';
		}
	}

	$player_lines[] = '==== THE STUDIO PLAYER – PLAYLIST-ÜBERSICHT & DESIGN ====';
	$player_lines[] = '[thespl.playlists]';

	$playlists = get_posts(
		array(
			'post_type'      => 'thespl_playlist',
			'post_status'    => array( 'publish', 'draft', 'private' ),
			'posts_per_page' => -1,
			'fields'         => 'ids',
		)
	);

	if ( ! empty( $playlists ) && is_array( $playlists ) ) {
		foreach ( $playlists as $playlist_id ) {
			$playlist_id = (int) $playlist_id;
			if ( $playlist_id <= 0 ) {
				continue;
			}

			$template_slug = get_post_meta( $playlist_id, '_thespl_playlist_template', true );
			$template_slug = is_string( $template_slug ) ? sanitize_key( $template_slug ) : '';
			if ( '' === $template_slug && function_exists( 'thespl_get_default_template_slug' ) ) {
				$template_slug = thespl_get_default_template_slug();
			}

			$json_raw = get_post_meta( $playlist_id, '_thespl_playlist_json', true );
			$json_raw = is_string( $json_raw ) ? $json_raw : '';

			$tracks_count = 0;

			$player_lines[] = 'playlist.' . $playlist_id . '.template = "' . $template_slug . '"';

			if ( '' !== $json_raw ) {
				$decoded = json_decode( $json_raw, true );
				if ( is_array( $decoded ) && isset( $decoded['tracks'] ) && is_array( $decoded['tracks'] ) ) {
					$tracks       = $decoded['tracks'];
					$tracks_count = count( $tracks );

					foreach ( $tracks as $track_index => $track ) {
						if ( ! is_array( $track ) ) {
							continue;
						}

						$labels = array();
						if ( isset( $track['variants'] ) && is_array( $track['variants'] ) ) {
							foreach ( $track['variants'] as $label ) {
								if ( ! is_string( $label ) ) {
									continue;
								}
								$label = trim( $label );
								if ( '' === $label ) {
									continue;
								}
								$labels[] = $label;
							}
						}

						$all_defaultish = true;
						if ( ! empty( $labels ) ) {
							foreach ( $labels as $label ) {
								if ( 1 !== preg_match( '/^Variant\s+\d+$/', $label ) ) {
									$all_defaultish = false;
									break;
								}
							}
						}

						if ( empty( $labels ) || $all_defaultish ) {
							$variants_out = 'Default';
						} else {
							$variants_out = implode( ', ', $labels );
						}

						$track_label    = 'track_' . ( $track_index + 1 );
						$player_lines[] = 'playlist.' . $playlist_id . '.' . $track_label . '.variants = "' . $variants_out . '"';
					}
				}
			}

			$player_lines[] = 'playlist.' . $playlist_id . '.tracks = ' . $tracks_count;
			$player_lines[] = '';
		}
	}

	$player_lines[] = '[thespl.playlists.design]';

	if ( ! empty( $playlists ) && is_array( $playlists ) ) {
		foreach ( $playlists as $playlist_id ) {
			$playlist_id = (int) $playlist_id;
			if ( $playlist_id <= 0 ) {
				continue;
			}

			$template_slug = get_post_meta( $playlist_id, '_thespl_playlist_template', true );
			$template_slug = is_string( $template_slug ) ? sanitize_key( $template_slug ) : '';
			if ( '' === $template_slug && function_exists( 'thespl_get_default_template_slug' ) ) {
				$template_slug = thespl_get_default_template_slug();
			}

			if ( function_exists( 'thespl_design_get_schema' ) ) {
				$design = thespl_design_get_schema( $playlist_id, $template_slug );
			} else {
				$design = get_post_meta( $playlist_id, '_thespl_playlist_design', true );
				$design = is_array( $design ) ? $design : array();
			}

			if ( ! is_array( $design ) ) {
				$design = array();
			}

			$prefix = 'playlist.' . $playlist_id . '.design.';

			if ( isset( $design['base_color'] ) && is_string( $design['base_color'] ) && '' !== $design['base_color'] ) {
				$player_lines[] = $prefix . 'base_color = "' . $design['base_color'] . '"';
			}
			if ( isset( $design['text_color'] ) && is_string( $design['text_color'] ) && '' !== $design['text_color'] ) {
				$player_lines[] = $prefix . 'text_color = "' . $design['text_color'] . '"';
			}
			if ( isset( $design['accent_color'] ) && is_string( $design['accent_color'] ) && '' !== $design['accent_color'] ) {
				$player_lines[] = $prefix . 'accent_color = "' . $design['accent_color'] . '"';
			}
			if ( isset( $design['accent_2_color'] ) && is_string( $design['accent_2_color'] ) && '' !== $design['accent_2_color'] ) {
				$player_lines[] = $prefix . 'accent_2_color = "' . $design['accent_2_color'] . '"';
			}
			if ( isset( $design['waveform_color'] ) && is_string( $design['waveform_color'] ) && '' !== $design['waveform_color'] ) {
				$player_lines[] = $prefix . 'waveform_color = "' . $design['waveform_color'] . '"';
			}
			if ( isset( $design['ring_base_color'] ) && is_string( $design['ring_base_color'] ) && '' !== $design['ring_base_color'] ) {
				$player_lines[] = $prefix . 'ring_base_color = "' . $design['ring_base_color'] . '"';
			}
			if ( isset( $design['button_bg_color'] ) && is_string( $design['button_bg_color'] ) && '' !== $design['button_bg_color'] ) {
				$player_lines[] = $prefix . 'button_bg_color = "' . $design['button_bg_color'] . '"';
			}
			if ( isset( $design['button_text_color'] ) && is_string( $design['button_text_color'] ) && '' !== $design['button_text_color'] ) {
				$player_lines[] = $prefix . 'button_text_color = "' . $design['button_text_color'] . '"';
			}
			if ( isset( $design['button_active_bg_color'] ) && is_string( $design['button_active_bg_color'] ) && '' !== $design['button_active_bg_color'] ) {
				$player_lines[] = $prefix . 'button_active_bg_color = "' . $design['button_active_bg_color'] . '"';
			}
			if ( isset( $design['button_active_text_color'] ) && is_string( $design['button_active_text_color'] ) && '' !== $design['button_active_text_color'] ) {
				$player_lines[] = $prefix . 'button_active_text_color = "' . $design['button_active_text_color'] . '"';
			}
			if ( isset( $design['chip_bg_color'] ) && is_string( $design['chip_bg_color'] ) && '' !== $design['chip_bg_color'] ) {
				$player_lines[] = $prefix . 'chip_bg_color = "' . $design['chip_bg_color'] . '"';
			}
			if ( isset( $design['chip_br_color'] ) && is_string( $design['chip_br_color'] ) && '' !== $design['chip_br_color'] ) {
				$player_lines[] = $prefix . 'chip_br_color = "' . $design['chip_br_color'] . '"';
			}
			if ( array_key_exists( 'buffer_ring_alpha', $design ) && is_numeric( $design['buffer_ring_alpha'] ) ) {
				$player_lines[] = $prefix . 'buffer_ring_alpha = ' . (float) $design['buffer_ring_alpha'];
			}
			if ( array_key_exists( 'show_playlist', $design ) ) {
				$player_lines[] = $prefix . 'show_playlist = ' . ( $design['show_playlist'] ? 'true' : 'false' );
			}
			if ( array_key_exists( 'show_audio_info', $design ) ) {
				$player_lines[] = $prefix . 'show_audio_info = ' . ( $design['show_audio_info'] ? 'true' : 'false' );
			}

			$player_lines[] = '';
		}
	}

	$player_lines[] = '==== THE STUDIO PLAYER – SUPPORT-DUMP – END ====';

	$plugin_lines = array( '==== PLUGIN-INVENTAR ====' );
	if ( ! function_exists( 'get_plugins' ) && file_exists( ABSPATH . 'wp-admin/includes/plugin.php' ) ) {
		/*
		 * Load the core plugin helper so we can call get_plugins() when building the
		 * support dump. This follows the WordPress.org guidelines: the helper file is
		 * required from within a function and its functions are used right away. It is
		 * not exposed as a separate loader or entry script.
		 */
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	}
	if ( function_exists( 'get_plugins' ) ) {
		$all_plugins = get_plugins();
		if ( ! empty( $all_plugins ) && is_array( $all_plugins ) ) {
			$plugin_lines[] = '';
			$plugin_lines[] = 'Installed Plugins:';
			foreach ( $all_plugins as $plugin_data ) {
				$name    = isset( $plugin_data['Name'] ) ? $plugin_data['Name'] : '';
				$version = isset( $plugin_data['Version'] ) ? $plugin_data['Version'] : '';
				if ( '' === $name && '' === $version ) {
					continue;
				}
				$line = $name;
				if ( '' !== $version ) {
					$line .= ' - v' . $version;
				}
				$plugin_lines[] = $line;
			}
		}
	}
	if ( ! empty( $plugin_lines ) ) {
		$system_report = implode( "\n", array_merge( $system_lines, $plugin_lines, $player_lines ) );
	} else {
		$system_report = implode( "\n", array_merge( $system_lines, $player_lines ) );
	}
	$opt_in_state                 = thespl_get_remote_opt_in_state();
	$remote_opt_in_enabled        = thespl_remote_opt_in_enabled();
	$uninstall_remove_uploads_raw = get_option( 'thespl_uninstall_remove_uploads', '' );
	if ( ! is_string( $uninstall_remove_uploads_raw ) ) {
		$uninstall_remove_uploads_raw = '';
	}
	$uninstall_remove_uploads = strtolower( trim( $uninstall_remove_uploads_raw ) );
	$uninstall_choice_delete  = ( 'yes' === $uninstall_remove_uploads );
	$uninstall_choice_keep    = ( 'no' === $uninstall_remove_uploads || '' === $uninstall_remove_uploads );

	$remote_docs       = $remote_opt_in_enabled ? thespl_get_remote_docs( 'dashboard', 'quickstart' ) : array();
	$remote_onboarding = $remote_opt_in_enabled ? thespl_get_remote_onboarding( 'dashboard' ) : array();
	$changelog_all     = $remote_opt_in_enabled ? thespl_get_remote_changelog( 5 ) : array();
	$date_format       = get_option( 'date_format' );
	if ( ! is_string( $date_format ) || '' === $date_format ) {
		$date_format = 'Y-m-d';
	}
	$settings          = thespl_get_player_settings();
	$onboarding_global = ! empty( $settings['onboarding_enabled'] );

	$primary_doc           = array();
	$primary_doc_body      = '';
	$primary_doc_url       = '';
	$primary_doc_vid       = '';
	$primary_doc_vid_embed = '';
	if ( ! empty( $remote_onboarding ) && is_array( $remote_onboarding ) ) {
		foreach ( $remote_onboarding as $doc ) {
			if ( ! is_array( $doc ) ) {
				continue;
			}
			$primary_doc = $doc;
			break;
		}
	}
	if ( ! empty( $primary_doc ) ) {
		if ( isset( $primary_doc['body'] ) && is_string( $primary_doc['body'] ) ) {
			$primary_doc_body = $primary_doc['body'];
		} elseif ( isset( $primary_doc['excerpt'] ) && is_string( $primary_doc['excerpt'] ) ) {
			$primary_doc_body = $primary_doc['excerpt'];
		}
		if ( isset( $primary_doc['docUrl'] ) && is_string( $primary_doc['docUrl'] ) ) {
			$primary_doc_url = $primary_doc['docUrl'];
		}
		if ( isset( $primary_doc['videoUrl'] ) && is_string( $primary_doc['videoUrl'] ) ) {
			$primary_doc_vid = $primary_doc['videoUrl'];
		}
	}
	if ( '' !== $primary_doc_vid && function_exists( 'thespl_normalize_onboarding_video_url' ) ) {
		$primary_doc_vid_embed = thespl_normalize_onboarding_video_url( $primary_doc_vid );
	}
	$has_onboarding_doc = ! empty( $primary_doc );

	if ( '' !== $primary_doc_body && false === strpos( $primary_doc_body, '<p' ) && false === strpos( $primary_doc_body, '<br' ) && function_exists( 'wpautop' ) ) {
		$primary_doc_body = wpautop( $primary_doc_body );
	}
	if ( function_exists( 'thespl_strip_remote_content_blocks' ) ) {
		$primary_doc_body = thespl_strip_remote_content_blocks( $primary_doc_body );
	}

	$show_onboarding = $onboarding_global && thespl_current_user_can_policy( 'plugin_access' );
	?>
	<div class="wrap thespl-admin-wrapper">
		<h1><?php echo esc_html( $page_title ); ?></h1>

		<?php thespl_render_remote_admin_banners(); ?>

		<?php thespl_render_admin_top_nav( 'the-studio-player' ); ?>

		<?php if ( $show_onboarding && '' === $opt_in_state ) : ?>
			<div class="thespl-onboarding-overlay" id="thespl-onboarding-overlay" data-thespl-overlay-state="pending" aria-hidden="true">
				<div class="thespl-onboarding-backdrop"></div>
				<div class="thespl-onboarding-dialog" role="dialog" aria-modal="true" aria-labelledby="thespl-onboarding-title">
					<div class="thespl-onboarding-header">
						<h2 id="thespl-onboarding-title"><?php esc_html_e( 'Welcome to The Studio Player', 'the-studio-player' ); ?></h2>
					</div>
					<div class="thespl-onboarding-body">
						<form method="post">
							<?php wp_nonce_field( 'thespl_remote_opt_in_choice', 'thespl_remote_opt_in_nonce' ); ?>
							<p><?php esc_html_e( 'The Studio Player can load dashboard content from studioplayer.de (onboarding, documentation, changelog and security notices).', 'the-studio-player' ); ?></p>
							<p><?php esc_html_e( 'To deliver the right content, requests include the plugin version, your plan (free/pro) and your site language (locale).', 'the-studio-player' ); ?></p>
							<p><?php esc_html_e( 'We do not send site content or user data. Standard HTTP metadata (for example your server IP address) is processed when fetching remote content.', 'the-studio-player' ); ?></p>
							<p><?php esc_html_e( 'You can change this decision at any time on the Settings page.', 'the-studio-player' ); ?></p>
							<p>
								<label>
									<input type="radio" name="thespl_remote_opt_in_choice" value="yes" checked>
									<?php esc_html_e( 'Yes, load remote dashboard content.', 'the-studio-player' ); ?>
								</label>
							</p>
							<p>
								<label>
									<input type="radio" name="thespl_remote_opt_in_choice" value="no">
									<?php esc_html_e( 'No, do not load external content. Open links on studioplayer.de instead.', 'the-studio-player' ); ?>
								</label>
							</p>
							<p><?php esc_html_e( 'You can also decide what should happen to The Studio Player uploads directory when uninstalling the plugin.', 'the-studio-player' ); ?></p>
							<p>
								<label>
									<input type="radio" name="thespl_uninstall_remove_uploads_choice" value="no" <?php checked( $uninstall_choice_keep, true ); ?>>
									<?php esc_html_e( 'Keep all files in wp-content/uploads/the-studio-player on uninstall (recommended).', 'the-studio-player' ); ?>
								</label>
							</p>
							<p>
								<label>
									<input type="radio" name="thespl_uninstall_remove_uploads_choice" value="yes" <?php checked( $uninstall_choice_delete, true ); ?>>
									<?php esc_html_e( 'Delete all files in wp-content/uploads/the-studio-player on uninstall.', 'the-studio-player' ); ?>
								</label>
							</p>
							<p class="thespl-uninstall-warning" data-thespl-uninstall-warning="dashboard" style="<?php echo $uninstall_choice_delete ? '' : 'display:none;'; ?>color:#b32d2e;font-weight:600;">
								<?php esc_html_e( 'Warning: the next time you delete this plugin, all Studio Player data and uploads will be permanently removed.', 'the-studio-player' ); ?>
							</p>
							<p>
								<button type="submit" class="button button-primary"><?php esc_html_e( 'Continue', 'the-studio-player' ); ?></button>
							</p>
						</form>
					</div>
				</div>
			</div>
		<?php elseif ( $show_onboarding && $remote_opt_in_enabled && $has_onboarding_doc ) : ?>
			<div class="thespl-onboarding-overlay" id="thespl-onboarding-overlay" data-thespl-overlay-state="pending" aria-hidden="true">
				<div class="thespl-onboarding-backdrop"></div>
				<div class="thespl-onboarding-dialog" role="dialog" aria-modal="true" aria-labelledby="thespl-onboarding-title">
					<div class="thespl-onboarding-header">
						<h2 id="thespl-onboarding-title"><?php esc_html_e( 'Welcome to The Studio Player', 'the-studio-player' ); ?></h2>
					</div>
					<?php
					$onboarding_intro     = '';
					$onboarding_body      = $primary_doc_body;
					$has_onboarding_intro = false;

					if ( '' !== $primary_doc_body ) {
						$onboarding_parts = explode( '<!-- tsp:onboarding-split -->', $primary_doc_body, 2 );

						if ( 2 === count( $onboarding_parts ) ) {
							$onboarding_intro     = $onboarding_parts[0];
							$onboarding_body      = $onboarding_parts[1];
							$has_onboarding_intro = '' !== trim( wp_strip_all_tags( $onboarding_intro ) );

							if ( ! $has_onboarding_intro ) {
								$onboarding_intro = '';
							}
						}
					}
					?>
					<div class="thespl-onboarding-body">
						<?php if ( '' !== $primary_doc_vid_embed && $has_onboarding_intro ) : ?>
							<div class="thespl-onboarding-top">
								<div class="thespl-onboarding-media">
									<iframe id="thespl-onboarding-video" src="<?php echo esc_url( $primary_doc_vid_embed ); ?>" allowfullscreen loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>
								</div>
								<div class="thespl-onboarding-intro">
									<?php echo wp_kses_post( $onboarding_intro ); ?>
								</div>
							</div>
						<?php elseif ( '' !== $primary_doc_vid_embed ) : ?>
							<div class="thespl-onboarding-media">
								<iframe id="thespl-onboarding-video" src="<?php echo esc_url( $primary_doc_vid_embed ); ?>" allowfullscreen loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>
							</div>
						<?php elseif ( $has_onboarding_intro ) : ?>
							<div class="thespl-onboarding-intro">
								<?php echo wp_kses_post( $onboarding_intro ); ?>
							</div>
						<?php endif; ?>
						<?php if ( '' !== $onboarding_body ) : ?>
							<div class="thespl-onboarding-content">
								<?php echo wp_kses_post( $onboarding_body ); ?>
							</div>
						<?php endif; ?>
					</div>
					<div class="thespl-onboarding-footer">
						<div class="thespl-onboarding-footer-left">
							<?php if ( '' !== $primary_doc_url ) : ?>
								<a href="<?php echo esc_url( $primary_doc_url ); ?>" target="_blank" class="button button-secondary"><?php esc_html_e( 'Open full guide', 'the-studio-player' ); ?></a>
							<?php endif; ?>
						</div>
						<div class="thespl-onboarding-footer-right">
							<button type="button" class="button" id="thespl-onboarding-disable"><?php esc_html_e( 'Don\'t show again', 'the-studio-player' ); ?></button>
							<button type="button" class="button button-primary" id="thespl-onboarding-close"><?php esc_html_e( 'Got it', 'the-studio-player' ); ?></button>
						</div>
					</div>
				</div>
			</div>
		<?php elseif ( $show_onboarding && 'no' === $opt_in_state ) : ?>
			<div class="thespl-onboarding-overlay" id="thespl-onboarding-overlay" data-thespl-overlay-state="pending" aria-hidden="true">
				<div class="thespl-onboarding-backdrop"></div>
				<div class="thespl-onboarding-dialog" role="dialog" aria-modal="true" aria-labelledby="thespl-onboarding-title">
					<div class="thespl-onboarding-header">
						<h2 id="thespl-onboarding-title"><?php esc_html_e( 'Welcome to The Studio Player', 'the-studio-player' ); ?></h2>
					</div>
					<div class="thespl-onboarding-body">
						<p><?php esc_html_e( 'Remote onboarding content is disabled for this site.', 'the-studio-player' ); ?></p>
						<p><?php esc_html_e( 'You can open the onboarding guide on our website instead.', 'the-studio-player' ); ?></p>
						<p>
							<a href="<?php echo esc_url( 'https://studioplayer.de/onboarding/' ); ?>" target="_blank" rel="noopener noreferrer" class="button button-secondary">
								<?php esc_html_e( 'Open onboarding on studioplayer.de', 'the-studio-player' ); ?>
							</a>
						</p>
						<p class="description">
							<?php
							$settings_url = admin_url( 'admin.php?page=thespl-player-settings' );
							$text         = wp_kses(
								/* translators: %s: Settings page URL. */
								__( 'You can enable remote onboarding content on the <a href="%s" id="thespl-onboarding-settings-link">Settings page</a>.', 'the-studio-player' ),
								array(
									'a' => array(
										'href' => true,
										'id'   => true,
									),
								)
							);
							printf( $text, esc_url( $settings_url ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
							?>
						</p>
					</div>
					<div class="thespl-onboarding-footer">
						<div class="thespl-onboarding-footer-right">
							<button type="button" class="button" id="thespl-onboarding-disable"><?php esc_html_e( 'Don\'t show again', 'the-studio-player' ); ?></button>
							<button type="button" class="button button-primary" id="thespl-onboarding-close"><?php esc_html_e( 'Got it', 'the-studio-player' ); ?></button>
						</div>
					</div>
				</div>
			</div>
		<?php endif; ?>

		<div class="section-box">
			<span class="section-title"><?php esc_html_e( 'Dashboard', 'the-studio-player' ); ?></span>
			<div class="thespl-admin-grid">
				<div class="thespl-admin-card">
					<h2><?php esc_html_e( 'System Status', 'the-studio-player' ); ?></h2>
					<div class="status-grid">
						<div class="status-item">
							<strong><?php esc_html_e( 'Plugin Version:', 'the-studio-player' ); ?></strong>
							<span><?php echo esc_html( $display_version ); ?></span>
						</div>
						<div class="status-item">
							<strong><?php esc_html_e( 'Edition:', 'the-studio-player' ); ?></strong>
							<span>
								<?php if ( 'Pro' === $license_label ) : ?>
									<span class="thespl-status-badge thespl-status-badge--pro"><?php esc_html_e( 'Pro', 'the-studio-player' ); ?></span>
								<?php else : ?>
									<?php if ( 'Free' === $license_label ) : ?>
										<span class="thespl-status-badge thespl-status-badge--free"><?php esc_html_e( 'Free', 'the-studio-player' ); ?></span>
									<?php else : ?>
										<span class="thespl-status-badge thespl-status-badge--pro"><?php echo esc_html( $license_label ); ?></span>
									<?php endif; ?>
								<?php endif; ?>
							</span>
						</div>
						<div class="status-item">
							<strong><?php esc_html_e( 'Plan:', 'the-studio-player' ); ?></strong>
							<span><?php echo esc_html( $license_plan_label ); ?></span>
						</div>
						<?php if ( $has_intro_offer && '' !== $license_extra_line && '' !== $license_extra_label ) : ?>
							<div class="status-item">
								<strong><?php echo esc_html( $license_extra_label . ':' ); ?></strong>
								<span><?php echo esc_html( $license_extra_line ); ?></span>
							</div>
						<?php endif; ?>
						<div class="status-item">
							<strong><?php esc_html_e( 'PHP Version:', 'the-studio-player' ); ?></strong>
							<span><?php echo esc_html( PHP_VERSION ); ?></span>
						</div>
						<div class="status-item">
							<strong><?php esc_html_e( 'WordPress Version:', 'the-studio-player' ); ?></strong>
							<span><?php echo esc_html( get_bloginfo( 'version' ) ); ?></span>
						</div>
						<div class="status-item">
							<strong><?php esc_html_e( 'Memory Limit:', 'the-studio-player' ); ?></strong>
							<span><?php echo esc_html( $memory_limit ); ?></span>
						</div>
						<div class="status-item">
							<strong><?php esc_html_e( 'Max execution time:', 'the-studio-player' ); ?></strong>
							<span><?php echo esc_html( $max_execution_time ); ?></span>
						</div>
						<div class="status-item">
							<strong><?php esc_html_e( 'Max POST size:', 'the-studio-player' ); ?></strong>
							<span><?php echo esc_html( $post_max_size ); ?></span>
						</div>
						<div class="status-item">
							<strong><?php esc_html_e( 'Upload max filesize:', 'the-studio-player' ); ?></strong>
							<span><?php echo esc_html( $upload_max_size ); ?></span>
						</div>
						<div class="status-item">
					<strong><?php esc_html_e( 'WP max upload size:', 'the-studio-player' ); ?></strong>
					<span><?php echo esc_html( $wp_upload_human ); ?></span>
				</div>
			</div>
			<p>
				<button type="button" class="button" id="thespl-system-report-copy"><?php esc_html_e( 'Copy system info', 'the-studio-player' ); ?></button>
				<span id="thespl-system-report-copied" class="thespl-admin-muted" style="display:none;">
					<?php esc_html_e( 'Copied.', 'the-studio-player' ); ?>
				</span>
			</p>
			<textarea id="thespl-system-report" readonly="readonly" style="position:absolute;left:-9999px;top:auto;width:1px;height:1px;overflow:hidden;"><?php echo esc_textarea( $system_report ); ?></textarea>
			<?php
			$latest_changelog = array();
			if ( ! empty( $changelog_all ) ) {
				$first_entry = $changelog_all[0];
				if ( is_array( $first_entry ) ) {
					$latest_changelog = $first_entry;
				}
			}
			if ( ! empty( $latest_changelog ) ) :
				$change_version           = '';
				$change_title             = '';
				$change_excerpt           = '';
				$change_body              = '';
				$change_published_display = '';
				$change_teaser_items      = array();
				$change_teaser_more       = false;
				if ( isset( $latest_changelog['version'] ) && is_string( $latest_changelog['version'] ) ) {
					$change_version = $latest_changelog['version'];
				}
				if ( isset( $latest_changelog['title'] ) && is_string( $latest_changelog['title'] ) ) {
					$change_title = $latest_changelog['title'];
				}
				if ( isset( $latest_changelog['body'] ) && is_string( $latest_changelog['body'] ) ) {
					$change_body = $latest_changelog['body'];
				} elseif ( isset( $latest_changelog['excerpt'] ) && is_string( $latest_changelog['excerpt'] ) ) {
					$change_body    = $latest_changelog['excerpt'];
					$change_excerpt = $latest_changelog['excerpt'];
				}
				if ( isset( $latest_changelog['publishedAt'] ) && is_string( $latest_changelog['publishedAt'] ) ) {
					$timestamp = strtotime( $latest_changelog['publishedAt'] );
					if ( $timestamp ) {
						$change_published_display = wp_date( $date_format, $timestamp );
					}
				}
				if ( '' === $change_version && '' !== $change_title ) {
					if ( preg_match( '/([0-9]+(?:\.[0-9]+){1,2})/', $change_title, $matches ) ) {
						$change_version = $matches[1];
					}
				}
				if ( '' !== $change_body ) {
					$items = array();
					if ( false !== stripos( $change_body, '<li' ) ) {
						$segments = preg_split( '/<li\b[^>]*>/i', $change_body );
						if ( is_array( $segments ) ) {
							foreach ( $segments as $index => $segment ) {
								if ( 0 === $index ) {
									continue;
								}
								$parts     = explode( '</li>', $segment );
								$item_html = $parts[0];
								$text      = wp_strip_all_tags( $item_html, false );
								$text      = wp_kses_decode_entities( $text );
								if ( ! is_string( $text ) ) {
									continue;
								}
								$text = preg_replace( '/\s+/', ' ', $text );
								$text = trim( $text );
								if ( '' === $text ) {
									continue;
								}
								$items[] = $text;
							}
						}
					}
					if ( empty( $items ) ) {
						$plain = wp_strip_all_tags( $change_body, false );
						$plain = wp_kses_decode_entities( $plain );
						if ( is_string( $plain ) ) {
							$plain = preg_replace( '/\s+/', ' ', $plain );
							$plain = trim( $plain );
						}
						if ( '' !== $plain ) {
							$items[] = $plain;
						}
					}
					if ( ! empty( $items ) ) {
						if ( count( $items ) > 2 ) {
							$change_teaser_more = true;
						}
						$change_teaser_items = array_slice( $items, 0, 2 );
					}
				}
				?>
			<hr />
			<div class="thespl-changelog-layout">
				<div class="thespl-changelog-layout__main">
					<h3><?php esc_html_e( 'What is new?', 'the-studio-player' ); ?></h3>
					<?php
					$change_header = '';
					if ( '' !== $change_version ) {
						$change_header = $change_version;
					}
					if ( '' !== $change_published_display ) {
						$change_header = '' !== $change_header ? ( $change_header . ' - ' . $change_published_display ) : $change_published_display;
					}
					?>
					<?php if ( '' !== $change_header ) : ?>
						<p><strong><?php echo esc_html( $change_header ); ?></strong></p>
					<?php endif; ?>
					<?php if ( ! empty( $change_teaser_items ) ) : ?>
						<ul class="thespl-changelog-teaser">
							<?php foreach ( $change_teaser_items as $item_text ) : ?>
								<li><?php echo esc_html( $item_text ); ?></li>
							<?php endforeach; ?>
							<?php if ( $change_teaser_more ) : ?>
								<li>&hellip;</li>
							<?php endif; ?>
						</ul>
					<?php endif; ?>
					<p>
						<button type="button" class="button" id="thespl-changelog-open">
							<?php esc_html_e( 'Show full changelog', 'the-studio-player' ); ?>
						</button>
					</p>
				</div>
			</div>
			<?php endif; ?>
			<?php if ( ! $remote_opt_in_enabled ) : ?>
				<p class="description">
					<?php esc_html_e( 'Remote changelog loading is disabled. You can read the changelog on our website instead.', 'the-studio-player' ); ?>
				</p>
				<p>
					<a href="<?php echo esc_url( 'https://studioplayer.de/changelog' ); ?>" target="_blank" rel="noopener noreferrer" class="button button-secondary">
						<?php esc_html_e( 'Open changelog on studioplayer.de', 'the-studio-player' ); ?>
					</a>
				</p>
				<p class="description">
					<?php esc_html_e( 'You can enable remote loading of the changelog on the Settings page.', 'the-studio-player' ); ?>
				</p>
			<?php endif; ?>
		</div>
		<div class="thespl-admin-card thespl-admin-card--docs" id="thespl-dashboard-quickstart-card">
					<h2><?php esc_html_e( 'Quickstart Guide', 'the-studio-player' ); ?></h2>
					<p><?php esc_html_e( 'Follow these steps to get started with The Studio Player.', 'the-studio-player' ); ?></p>
					<?php if ( $remote_opt_in_enabled && ! empty( $remote_docs ) ) : ?>
						<div class="thespl-admin-docs-scroll">
							<ul class="thespl-admin-doc-links">
										<?php foreach ( $remote_docs as $doc ) : ?>
											<?php
											if ( ! is_array( $doc ) ) {
												continue;
											}
											$doc_title  = '';
											$doc_url    = '';
											$doc_target = '_self';
											$doc_body   = '';

											if ( isset( $doc['title'] ) && is_string( $doc['title'] ) ) {
												$doc_title = $doc['title'];
											}
											if ( isset( $doc['docUrl'] ) && is_string( $doc['docUrl'] ) ) {
												$doc_url = $doc['docUrl'];
											}
											if ( isset( $doc['docTarget'] ) && is_string( $doc['docTarget'] ) ) {
												$doc_target = ( '_blank' === $doc['docTarget'] ) ? '_blank' : '_self';
											}
											if ( isset( $doc['body'] ) && is_string( $doc['body'] ) ) {
												$doc_body = $doc['body'];
											} elseif ( isset( $doc['excerpt'] ) && is_string( $doc['excerpt'] ) ) {
												$doc_body = $doc['excerpt'];
											}
											if ( '' !== $doc_body && function_exists( 'thespl_strip_remote_content_blocks' ) ) {
												$doc_body = thespl_strip_remote_content_blocks( $doc_body );
											}
											if ( '' === $doc_title && '' === $doc_url ) {
												continue;
											}
											?>
								<li>
											<?php if ( '' !== $doc_body ) : ?>
										<span class="description"><?php echo wp_kses_post( $doc_body ); ?></span>
									<?php endif; ?>
											<?php if ( '' !== $doc_url ) : ?>
										<br /><a href="<?php echo esc_url( $doc_url ); ?>" target="<?php echo esc_attr( $doc_target ); ?>"><?php esc_html_e( 'Open full documentation', 'the-studio-player' ); ?></a>
									<?php endif; ?>
								</li>
							<?php endforeach; ?>
							</ul>
						</div>
					<?php else : ?>
						<p class="description">
							<?php esc_html_e( 'Remote documentation loading is disabled. You can read the Quickstart guide on our website instead.', 'the-studio-player' ); ?>
						</p>
						<p>
							<a href="<?php echo esc_url( 'https://studioplayer.de/docs/quick-start/' ); ?>" target="_blank" rel="noopener noreferrer" class="button button-secondary">
								<?php esc_html_e( 'Open Quickstart on studioplayer.de', 'the-studio-player' ); ?>
							</a>
						</p>
						<p class="description">
							<?php esc_html_e( 'You can enable remote loading of the Quickstart guide on the Settings page.', 'the-studio-player' ); ?>
						</p>
					<?php endif; ?>
				</div>
					<div class="thespl-admin-card" id="thespl-dashboard-links-card">
					<h2><?php esc_html_e( 'Quick Links & Support', 'the-studio-player' ); ?></h2>
					<p><?php esc_html_e( 'Access key tools or open the full documentation on the Help page.', 'the-studio-player' ); ?></p>
					<div class="quick-links">
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=thespl-muxer' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Open Muxer-Uploader', 'the-studio-player' ); ?></a>
						<a href="<?php echo esc_url( admin_url( 'edit.php?post_type=thespl_playlist' ) ); ?>" class="button"><?php esc_html_e( 'Build a Player', 'the-studio-player' ); ?></a>
						<?php if ( thespl_current_user_can_policy( 'plugin_access' ) ) : ?>
							<a href="<?php echo esc_url( admin_url( 'admin.php?page=thespl-player-settings' ) ); ?>" class="button"><?php esc_html_e( 'Settings', 'the-studio-player' ); ?></a>
						<?php else : ?>
							<span class="button button-secondary thespl-button-disabled" title="<?php echo esc_attr__( 'To gain access, contact your administrator.', 'the-studio-player' ); ?>" aria-disabled="true">
								<span class="thespl-admin-nav-tab-label"><?php esc_html_e( 'Settings', 'the-studio-player' ); ?></span>
								<span class="thespl-admin-nav-tab-lock"><?php esc_html_e( '🔒 Restricted', 'the-studio-player' ); ?></span>
							</span>
						<?php endif; ?>
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=thespl-help' ) ); ?>" class="button"><?php esc_html_e( 'Help & Docs', 'the-studio-player' ); ?></a>
					</div>
				<hr />
				<div class="thespl-quick-links-grid">
					<div class="thespl-quick-links-item">
						<h3><?php esc_html_e( 'Support', 'the-studio-player' ); ?></h3>
						<p>
							<span>
								<?php echo esc_html( '💬 ' ); ?>
								<a href="<?php echo esc_url( thespl_get_support_url() ); ?>" target="_blank" rel="noopener">
									<?php echo esc_html( 'studioplayer.de/support/' ); ?>
								</a>
							</span><br />
						</p>
						<?php if ( '' !== $support_status_text ) : ?>
							<div class="thespl-license-status <?php echo esc_attr( $support_tone_class ? $support_tone_class : 'thespl-license-status--neutral' ); ?>">
								<span class="thespl-license-status__badge"><?php echo esc_html( $license_label ); ?></span>
								<span class="thespl-license-status__text"><?php echo esc_html( $support_status_text ); ?></span>
							</div>
						<?php endif; ?>
					</div>
					<div class="thespl-quick-links-item">
						<h3><?php esc_html_e( 'Newsletter', 'the-studio-player' ); ?></h3>
						<p>
							<span>
								<?php echo esc_html( '🔔 ' ); ?>
								<?php esc_html_e( 'Updates & new features', 'the-studio-player' ); ?>
							</span><br />
							<span>
								<?php echo esc_html( '→ ' ); ?>
								<a href="<?php echo esc_url( 'https://studioplayer.de/newsletter' ); ?>" target="_blank" rel="noopener noreferrer">
									<?php esc_html_e( 'Subscribe', 'the-studio-player' ); ?>
								</a>
							</span>
						</p>
					</div>
					<div class="thespl-quick-links-item">
						<h3><?php esc_html_e( 'Legal', 'the-studio-player' ); ?></h3>
						<p>
							<span>
								<?php esc_html_e( 'Developer: Peter Rahe', 'the-studio-player' ); ?>
							</span><br />
							<span>
								<?php echo esc_html( '→ ' ); ?>
								<a href="<?php echo esc_url( 'https://studioplayer.de/imprint' ); ?>" target="_blank" rel="noopener noreferrer">
									<?php esc_html_e( 'Imprint & Privacy', 'the-studio-player' ); ?>
								</a>
							</span>
						</p>
					</div>
					<?php if ( ! thespl_is_pro_active() ) : ?>
						<div class="thespl-quick-links-item">
							<h3><?php esc_html_e( 'Upgrade to Pro', 'the-studio-player' ); ?></h3>
							<p>
								<span class="description">
									<?php esc_html_e( 'Unlock advanced features for professional use.', 'the-studio-player' ); ?>
								</span><br />
								<span>
									<a href="<?php echo esc_url( thespl_get_marketing_pricing_url() ); ?>" target="_blank" rel="noopener noreferrer" class="thespl-license-status__cta">
										<?php esc_html_e( 'Get Pro', 'the-studio-player' ); ?>
									</a>
								</span>
							</p>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>
		<?php if ( ! empty( $changelog_all ) ) : ?>
		<div id="thespl-changelog-modal" class="thespl-changelog-modal" style="display:none; position:fixed; z-index:100000; left:0; top:0; width:100%; height:100%; overflow:auto; background:rgba(0,0,0,0.5);">
			<div class="thespl-changelog-modal-inner" style="background:#fff; margin:40px auto; padding:20px; max-width:800px; box-shadow:0 2px 10px rgba(0,0,0,0.2);">
				<h2><?php esc_html_e( 'Changelog', 'the-studio-player' ); ?></h2>
				<button type="button" class="button" id="thespl-changelog-close" style="float:right; margin-top:-36px;">
					<?php esc_html_e( 'Close', 'the-studio-player' ); ?>
				</button>
				<div class="thespl-changelog-entries">
					<?php foreach ( $changelog_all as $entry ) : ?>
						<?php
						if ( ! is_array( $entry ) ) {
							continue;
						}
						$entry_version           = '';
						$entry_title             = '';
						$entry_body              = '';
						$entry_published         = '';
						$entry_published_display = '';
						if ( isset( $entry['version'] ) && is_string( $entry['version'] ) ) {
							$entry_version = $entry['version'];
						}
						if ( isset( $entry['title'] ) && is_string( $entry['title'] ) ) {
							$entry_title = $entry['title'];
						}
						if ( isset( $entry['body'] ) && is_string( $entry['body'] ) ) {
							$entry_body = $entry['body'];
						} elseif ( isset( $entry['excerpt'] ) && is_string( $entry['excerpt'] ) ) {
							$entry_body = $entry['excerpt'];
						}
						if ( isset( $entry['publishedAt'] ) && is_string( $entry['publishedAt'] ) ) {
							$entry_published = $entry['publishedAt'];
							$timestamp       = strtotime( $entry_published );
							if ( $timestamp ) {
								$entry_published_display = wp_date( $date_format, $timestamp );
							}
						}
						if ( '' === $entry_version && '' !== $entry_title ) {
							if ( preg_match( '/([0-9]+(?:\.[0-9]+){1,2})/', $entry_title, $matches ) ) {
								$entry_version = $matches[1];
							}
						}
						$entry_header = '';
						if ( '' !== $entry_version ) {
							$entry_header = $entry_version;
						}
						if ( '' !== $entry_published_display ) {
							$entry_header = '' !== $entry_header ? ( $entry_header . ' - ' . $entry_published_display ) : $entry_published_display;
						}
						if ( '' === $entry_header ) {
							$entry_header = $entry_title;
						}
						?>
					<div class="thespl-changelog-entry" style="margin-bottom:20px;">
						<h3><?php echo esc_html( $entry_header ); ?></h3>
							<?php if ( '' !== $entry_body ) : ?>
								<div class="description"><?php echo wp_kses_post( $entry_body ); ?></div>
							<?php endif; ?>
						</div>
					<?php endforeach; ?>
				</div>
			</div>
		</div>
	<?php endif; ?>
	<?php
}

/**
 * Render Help page.
 *
 * @return void
 */
function thespl_render_help_page() {
	if ( ! thespl_current_user_can_policy( 'plugin_access' ) ) {
		return;
	}

	$ver           = thespl_get_readme_stable_tag_version();
	$plugin_name   = __( 'The Studio Player', 'the-studio-player' );
	$page_label    = __( 'Help', 'the-studio-player' );
	$page_title    = $plugin_name;
	$profile       = thespl_get_capability_profile();
	$has_pro_build = isset( $profile['is_pro'] ) ? (bool) $profile['is_pro'] : false;
	if ( '' !== $ver ) {
		$page_title .= ' - ' . $ver;
		if ( $has_pro_build ) {
			$page_title .= ' Pro';
		}
	}
	$page_title           .= ' - ' . $page_label;
	$opt_in_state          = thespl_get_remote_opt_in_state();
	$remote_opt_in_enabled = thespl_remote_opt_in_enabled();
	$remote_docs           = $remote_opt_in_enabled ? thespl_get_remote_docs( 'dashboard', 'full' ) : array();
	$changelog_all         = $remote_opt_in_enabled ? thespl_get_remote_changelog( 10 ) : array();
	$date_format           = get_option( 'date_format' );
	if ( ! is_string( $date_format ) || '' === $date_format ) {
		$date_format = 'Y-m-d';
	}
	?>
	<div class="wrap thespl-admin-wrapper">
		<h1><?php echo esc_html( $page_title ); ?></h1>

		<?php thespl_render_remote_admin_banners(); ?>

		<?php thespl_render_admin_top_nav( 'thespl-help' ); ?>

		<div class="section-box thespl-help-section">
			<span class="section-title"><?php esc_html_e( 'Help & Documentation', 'the-studio-player' ); ?></span>
			<?php if ( $remote_opt_in_enabled && ! empty( $remote_docs ) ) : ?>
				<div class="thespl-admin-grid thespl-help-docs-grid">
							<?php foreach ( $remote_docs as $doc ) : ?>
								<?php
								if ( ! is_array( $doc ) ) {
									continue;
								}
								$doc_title  = '';
								$doc_url    = '';
								$doc_target = '_self';
								$doc_body   = '';

								if ( isset( $doc['title'] ) && is_string( $doc['title'] ) ) {
									$doc_title = $doc['title'];
								}
								if ( isset( $doc['docUrl'] ) && is_string( $doc['docUrl'] ) ) {
									$doc_url = $doc['docUrl'];
								}
								if ( isset( $doc['docTarget'] ) && is_string( $doc['docTarget'] ) ) {
									$doc_target = ( '_blank' === $doc['docTarget'] ) ? '_blank' : '_self';
								}
								if ( isset( $doc['body'] ) && is_string( $doc['body'] ) ) {
									$doc_body = $doc['body'];
								} elseif ( isset( $doc['excerpt'] ) && is_string( $doc['excerpt'] ) ) {
									$doc_body = $doc['excerpt'];
								}
								if ( '' !== $doc_body && function_exists( 'thespl_strip_remote_content_blocks' ) ) {
									$doc_body = thespl_strip_remote_content_blocks( $doc_body );
								}
								if ( '' === $doc_title && '' === $doc_url ) {
									continue;
								}
								?>
						<div class="thespl-admin-card">
							<h2><?php echo esc_html( $doc_title ); ?></h2>
								<?php if ( '' !== $doc_body ) : ?>
									<div class="description"><?php echo wp_kses_post( $doc_body ); ?></div>
								<?php endif; ?>
								<?php if ( '' !== $doc_url ) : ?>
									<p><a href="<?php echo esc_url( $doc_url ); ?>" target="<?php echo esc_attr( $doc_target ); ?>"><?php esc_html_e( 'Open on website', 'the-studio-player' ); ?></a></p>
								<?php endif; ?>
						</div>
					<?php endforeach; ?>
				</div>
			<?php else : ?>
				<p class="description">
					<?php esc_html_e( 'Remote documentation loading is disabled. You can read the full documentation on our website instead.', 'the-studio-player' ); ?>
				</p>
				<p>
					<a href="<?php echo esc_url( 'https://studioplayer.de/docs/user-guide/' ); ?>" target="_blank" rel="noopener noreferrer" class="button button-secondary">
						<?php esc_html_e( 'Open documentation on studioplayer.de', 'the-studio-player' ); ?>
					</a>
				</p>
				<p class="description">
					<?php esc_html_e( 'You can enable remote loading of documentation on the Settings page.', 'the-studio-player' ); ?>
				</p>
			<?php endif; ?>
		</div>
	</div>
	<?php
}
