<?php
/**
 * Admin Menu and Navigation.
 *
 * @package TheStudioPlayer
 */

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

/**
 * Check if a plugin action link is a Freemius opt-in/reconnect link.
 *
 * @param string $link_html Plugin action link HTML.
 * @return bool
 */
function thespl_is_freemius_optin_action_link( $link_html ) {
	if ( ! is_string( $link_html ) || '' === $link_html ) {
		return false;
	}

	return (
		false !== strpos( $link_html, 'opt-in-or-opt-out' ) ||
		false !== strpos( $link_html, 'fs_action=reset_anonymous_mode' ) ||
		false !== strpos( $link_html, 'fs_action=the-studio-player_reconnect' ) ||
		false !== strpos( $link_html, 'fs_action=reset_pending_activation_mode' )
	);
}

/**
 * Filter action links for The Studio Player in the plugins list.
 *
 * @param string[] $links Action links.
 * @return string[]
 */
function thespl_add_plugin_action_links( $links ) {
	if ( ! thespl_current_user_can_policy( 'plugin_access' ) ) {
		return $links;
	}

	$has_pro_assets = function_exists( 'thespl_has_pro_assets' ) ? (bool) thespl_has_pro_assets() : false;
	if ( ! $has_pro_assets ) {
		$filtered_links = array();
		foreach ( $links as $link_key => $link_html ) {
			if ( ! is_string( $link_html ) ) {
				$filtered_links[ $link_key ] = $link_html;
				continue;
			}

			if ( thespl_is_freemius_optin_action_link( $link_html ) ) {
				continue;
			}

			$filtered_links[ $link_key ] = $link_html;
		}

		$links = $filtered_links;
	}

	$dashboard_link = '<a href="' . esc_url( admin_url( 'admin.php?page=the-studio-player' ) ) . '">' . esc_html__( 'Dashboard', 'the-studio-player' ) . '</a>';
	$links[]        = $dashboard_link;

	if ( ! $has_pro_assets ) {
		$get_pro_url  = thespl_get_marketing_pricing_url();
		$get_pro_link = '<a href="' . esc_url( $get_pro_url ) . '" target="_blank" rel="noopener noreferrer" style="color:#C6730D;font-weight:600;">' . esc_html__( 'Get Pro', 'the-studio-player' ) . '</a>';
		$links[]      = $get_pro_link;
	}

	return $links;
}
$thespl_action_links_file = defined( 'THESPL_PLUGIN_MAIN_FILE' ) ? THESPL_PLUGIN_MAIN_FILE : ( THESPL_PLUGIN_DIR . 'the-studio-player.php' );
add_filter( 'plugin_action_links_' . plugin_basename( $thespl_action_links_file ), 'thespl_add_plugin_action_links', 999 );
add_filter( 'network_admin_plugin_action_links_' . plugin_basename( $thespl_action_links_file ), 'thespl_add_plugin_action_links', 999 );

/**
 * Add row meta links in the plugin list.
 *
 * @param string[] $links Row meta links.
 * @param string   $file  Plugin file.
 * @return string[]
 */
function thespl_add_plugin_row_meta( $links, $file ) {
	$main = defined( 'THESPL_PLUGIN_MAIN_FILE' ) ? THESPL_PLUGIN_MAIN_FILE : ( THESPL_PLUGIN_DIR . 'the-studio-player.php' );
	if ( plugin_basename( $main ) !== $file ) {
		return $links;
	}

	foreach ( $links as $index => $link_html ) {
		if ( ! is_string( $link_html ) ) {
			continue;
		}
		if ( false !== strpos( $link_html, 'studioplayer.de' ) && false === strpos( $link_html, 'mtm_campaign=' ) ) {
			unset( $links[ $index ] );
			continue;
		}
		if ( false !== strpos( $link_html, 'peter-rahe.de' ) && false === strpos( $link_html, 'target=' ) ) {
			$author_url      = 'https://www.peter-rahe.de';
			$author_name     = 'Peter Rahe';
			$links[ $index ] = '<a href="' . esc_url( $author_url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $author_name ) . '</a>';
		}
	}

	$dashboard_link = '<a href="' . esc_url( admin_url( 'admin.php?page=the-studio-player' ) ) . '">' . esc_html__( 'Dashboard', 'the-studio-player' ) . '</a>';
	$website_url    = thespl_get_marketing_website_url();
	$website_link   = '<a href="' . esc_url( $website_url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Plugin Website', 'the-studio-player' ) . '</a>';
	$reviews_url    = 'https://wordpress.org/support/plugin/the-studio-player/reviews/';
	$reviews_link   = '<a href="' . esc_url( $reviews_url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Review on WordPress.org', 'the-studio-player' ) . '</a>';
	$get_pro_url    = thespl_get_marketing_pricing_url();
	$get_pro_label  = esc_html__( 'Get Pro', 'the-studio-player' );
	$get_pro_link   = '<a href="' . esc_url( $get_pro_url ) . '" target="_blank" rel="noopener noreferrer" style="color:#C6730D;font-weight:600;">' . $get_pro_label . '</a>';
	$links[]        = $dashboard_link;
	$links[]        = $website_link;
	$links[]        = $reviews_link;
	$has_pro_assets = function_exists( 'thespl_has_pro_assets' ) ? (bool) thespl_has_pro_assets() : false;

	if ( ! $has_pro_assets ) {
		$links[] = $get_pro_link;
	}

	return $links;
}
add_filter( 'plugin_row_meta', 'thespl_add_plugin_row_meta', 10, 2 );

/**
 * Read the current plugin admin page slug from the current admin context.
 *
 * @return string Requested page slug or empty string.
 */
function thespl_get_admin_page_request_value() {
	global $plugin_page;

	return is_string( $plugin_page ) ? sanitize_key( $plugin_page ) : '';
}

/**
 * Resolve the current plugin admin page slug from the request.
 *
 * @return string
 */
function thespl_get_current_plugin_admin_page() {
	$allowed_pages = array(
		'the-studio-player',
		'thespl-muxer',
		'thespl-player-settings',
		'thespl-tools',
		'thespl-license',
		'thespl-help',
		'the-studio-player-account',
	);

	$page = thespl_get_admin_page_request_value();
	if ( '' === $page ) {
		return '';
	}

	if ( ! in_array( $page, $allowed_pages, true ) ) {
		return '';
	}

	return $page;
}

/**
 * Read the targeted admin post ID from the current admin context.
 *
 * @return int Requested post ID or zero.
 */
function thespl_get_admin_post_request_value() {
	global $post;
	global $post_ID;

	if ( $post instanceof WP_Post ) {
		return (int) $post->ID;
	}

	if ( is_numeric( $post_ID ) ) {
		return absint( $post_ID );
	}

	return 0;
}

/**
 * Resolve the requested playlist post ID from the admin query string.
 *
 * @return int
 */
function thespl_get_requested_admin_post_id() {
	return thespl_get_admin_post_request_value();
}

/**
 * Read the media list source filter from the current request.
 *
 * @return string Requested source filter or empty string.
 */
function thespl_get_media_list_source_request_value() {
	if ( ! isset( $_GET['thespl_source'] ) ) {
		return '';
	}

	if ( ! isset( $_GET['thespl_media_list_nonce'] ) ) {
		return '';
	}

	$nonce = sanitize_text_field( wp_unslash( $_GET['thespl_media_list_nonce'] ) );
	if ( '' === $nonce || ! wp_verify_nonce( $nonce, 'thespl_media_list_filter' ) ) {
		return '';
	}

	$source = isset( $_GET['thespl_source'] ) ? sanitize_text_field( wp_unslash( $_GET['thespl_source'] ) ) : '';

	return '' !== $source ? sanitize_key( $source ) : '';
}

/**
 * Resolve the media library source filter for The Studio Player uploads.
 *
 * @return string
 */
function thespl_get_media_list_source_filter() {
	$allowed_sources = array(
		'',
		'thespl',
	);

	$source = thespl_get_media_list_source_request_value();
	if ( '' === $source ) {
		return '';
	}

	if ( ! in_array( $source, $allowed_sources, true ) ) {
		return '';
	}

	return $source;
}

/**
 * Check whether the current admin screen belongs to The Studio Player.
 *
 * @return bool
 */
function thespl_is_plugin_admin_screen() {
	$page  = thespl_get_current_plugin_admin_page();
	$pages = array(
		'the-studio-player',
		'thespl-muxer',
		'thespl-player-settings',
		'thespl-tools',
		'thespl-license',
		'thespl-help',
	);
	if ( '' !== $page && in_array( $page, $pages, true ) ) {
		return true;
	}
	if ( function_exists( 'get_current_screen' ) ) {
		$screen = get_current_screen();
		if ( $screen && isset( $screen->post_type ) && 'thespl_playlist' === $screen->post_type ) {
			return true;
		}
	}
	return false;
}

/**
 * Override admin update footer with plugin version on plugin pages only.
 *
 * @param string $text    Original footer text.
 * @param string $version WordPress version.
 * @return string
 */
function thespl_admin_update_footer_override( $text, $version = '' ) {
	if ( ! thespl_is_plugin_admin_screen() ) {
		return $text;
	}
	unset( $version );
	$ver     = thespl_get_readme_stable_tag_version();
	$profile = thespl_get_capability_profile();
	$is_pro  = isset( $profile['is_pro'] ) ? (bool) $profile['is_pro'] : false;
	$suffix  = $is_pro ? ' Pro' : '';
	return sprintf( '%s %s%s', __( 'Version', 'the-studio-player' ), esc_html( $ver ), esc_html( $suffix ) );
}
add_filter( 'update_footer', 'thespl_admin_update_footer_override', 999 );

/**
 * Override admin footer text on plugin pages only.
 *
 * @param string $text Original footer text.
 * @return string
 */
function thespl_admin_footer_text_override( $text ) {
	if ( ! thespl_is_plugin_admin_screen() ) {
		return $text;
	}
	return 'The Studio Player';
}
add_filter( 'admin_footer_text', 'thespl_admin_footer_text_override', 999 );

/**
 * Provide default admin brand logo URL.
 *
 * @param string $url Current logo URL.
 * @return string
 */
function thespl_admin_brand_logo_default( $url ) {
	unset( $url );
	$svg_path = THESPL_PLUGIN_DIR . 'assets/img/thespl_logo.svg';
	if ( file_exists( $svg_path ) ) {
		return THESPL_PLUGIN_URL . 'assets/img/thespl_logo.svg';
	}
	return THESPL_PLUGIN_URL . 'assets/img/thespl_logo.png';
}
add_filter( 'thespl_admin_brand_logo_url', 'thespl_admin_brand_logo_default' );

/**
 * Register Admin Menu.
 */
function thespl_register_admin_menu() {
	$cap_plugin_access  = thespl_get_policy_capability( 'plugin_access' );
	$cap_settings       = thespl_get_policy_capability( 'plugin_access' );
	$cap_tools          = thespl_get_policy_capability( 'tools_diagnose' );
	$cap_license_view   = thespl_get_policy_capability( 'license_view' );
	$cap_license_manage = thespl_get_policy_capability( 'license_manage' );
	$cap_plugin_access  = '' !== $cap_plugin_access ? $cap_plugin_access : 'manage_options';
	$cap_settings       = '' !== $cap_settings ? $cap_settings : 'manage_options';
	$cap_tools          = '' !== $cap_tools ? $cap_tools : 'manage_options';
	$cap_license_view   = '' !== $cap_license_view ? $cap_license_view : 'manage_options';
	$cap_license_manage = '' !== $cap_license_manage ? $cap_license_manage : 'manage_options';
	$profile            = thespl_get_capability_profile();
	$is_pro             = isset( $profile['is_pro'] ) ? (bool) $profile['is_pro'] : false;
	$has_pro_assets     = function_exists( 'thespl_has_pro_assets' ) ? (bool) thespl_has_pro_assets() : false;
	$license_menu_label = __( 'License', 'the-studio-player' );
	if ( ! $has_pro_assets ) {
		$license_menu_label = __( 'Get Pro', 'the-studio-player' );
	}

	// Top level menu.
	$menu_title_top = $is_pro ? __( 'Studio Player Pro', 'the-studio-player' ) : __( 'Studio Player', 'the-studio-player' );
	$page_title_top = $is_pro ? __( 'Studio Player Pro', 'the-studio-player' ) : __( 'Studio Player', 'the-studio-player' );
	add_menu_page(
		$page_title_top,
		$menu_title_top,
		$cap_plugin_access,
		'the-studio-player',
		'thespl_render_dashboard_page',
		'dashicons-media-audio',
		81
	);

	// Rename first submenu item.
	add_submenu_page(
		'the-studio-player',
		__( 'Dashboard', 'the-studio-player' ),
		__( 'Dashboard', 'the-studio-player' ),
		$cap_plugin_access,
		'the-studio-player',
		'thespl_render_dashboard_page'
	);

	// Muxer submenu.
	$menu_title = __( 'Muxer-Uploader', 'the-studio-player' );
	add_submenu_page(
		'the-studio-player',
		$menu_title,
		$menu_title,
		$cap_plugin_access,
		'thespl-muxer',
		'thespl_render_muxer_page'
	);

	add_submenu_page(
		'the-studio-player',
		__( 'Settings', 'the-studio-player' ),
		__( 'Settings', 'the-studio-player' ),
		$cap_settings,
		'thespl-player-settings',
		'thespl_render_player_settings_page'
	);

	add_submenu_page(
		'the-studio-player',
		__( 'Tools', 'the-studio-player' ),
		__( 'Tools', 'the-studio-player' ),
		$cap_tools,
		'thespl-tools',
		'thespl_render_tools_page'
	);

	add_submenu_page(
		'the-studio-player',
		__( 'Help', 'the-studio-player' ),
		__( 'Help', 'the-studio-player' ),
		$cap_plugin_access,
		'thespl-help',
		'thespl_render_help_page'
	);

	add_submenu_page(
		'the-studio-player',
		$license_menu_label,
		$license_menu_label,
		$cap_license_view,
		'thespl-license',
		'thespl_render_license_page'
	);
}
add_action( 'admin_menu', 'thespl_register_admin_menu' );

/**
 * Reorder plugin submenus in the WordPress admin.
 *
 * @return void
 */
function thespl_reorder_admin_submenus() {
	global $submenu;
	$parent = 'the-studio-player';
	if ( ! isset( $submenu[ $parent ] ) || ! is_array( $submenu[ $parent ] ) ) {
		return;
	}
	$items = $submenu[ $parent ];
	$map   = array();
	foreach ( $items as $item ) {
		$slug         = isset( $item[2] ) ? (string) $item[2] : '';
		$map[ $slug ] = $item;
	}
	$ordered = array();
	$want    = array(
		'the-studio-player',
		'thespl-muxer',
		'edit.php?post_type=thespl_playlist',
		'thespl-player-settings',
		'thespl-tools',
		'thespl-help',
		'thespl-license',
	);
	foreach ( $want as $slug ) {
		if ( isset( $map[ $slug ] ) ) {
			$ordered[] = $map[ $slug ];
			unset( $map[ $slug ] );
		}
	}
	foreach ( $map as $rest ) {
		$ordered[] = $rest;
	}
	$submenu_ref =& $submenu[ $parent ];
	array_splice( $submenu_ref, 0, count( $submenu_ref ), $ordered );
}
add_action( 'admin_menu', 'thespl_reorder_admin_submenus', 100 );

/**
 * Add admin body class for highlighting the Get Pro submenu for free users.
 *
 * @param string $classes Existing admin body classes.
 * @return string
 */
function thespl_add_license_highlight_body_class( $classes ) {
	$has_pro_assets = function_exists( 'thespl_has_pro_assets' ) ? (bool) thespl_has_pro_assets() : false;
	if ( $has_pro_assets ) {
		return $classes;
	}

	if ( strpos( $classes, 'thespl-license-highlight' ) === false ) {
		$classes .= ' thespl-license-highlight';
	}

	return $classes;
}
add_filter( 'admin_body_class', 'thespl_add_license_highlight_body_class' );

/**
 * Build human-readable lock tag text for a policy capability.
 *
 * @param string $policy_key   Policy key.
 * @param string $default_text Fallback label when capability is unknown.
 * @return string
 */
function thespl_get_policy_locked_tag_label( $policy_key, $default_text ) {
	$default_text = is_string( $default_text ) ? $default_text : '';
	$capability   = thespl_get_policy_capability( $policy_key );

	if ( 'manage_options' === $capability ) {
		return __( 'Admin only', 'the-studio-player' );
	}

	if ( 'edit_others_posts' === $capability ) {
		return __( 'Editor/Admin', 'the-studio-player' );
	}

	if ( 'publish_posts' === $capability ) {
		return __( 'Author/Editor/Admin', 'the-studio-player' );
	}

	return '' !== $default_text ? $default_text : __( 'Restricted', 'the-studio-player' );
}

/**
 * Render the admin top navigation tabs.
 *
 * @param string $active_slug Active menu slug.
 * @return void
 */
function thespl_render_admin_top_nav( $active_slug ) {
	$active = is_string( $active_slug ) ? $active_slug : '';
	$items  = array();

	if ( thespl_current_user_can_policy( 'plugin_access' ) ) {
		$items[] = array(
			'slug'  => 'the-studio-player',
			'label' => __( 'Dashboard', 'the-studio-player' ),
			'href'  => admin_url( 'admin.php?page=the-studio-player' ),
		);
		$items[] = array(
			'slug'  => 'thespl-muxer',
			'label' => __( 'Muxer-Uploader', 'the-studio-player' ),
			'href'  => admin_url( 'admin.php?page=thespl-muxer' ),
		);
		$items[] = array(
			'slug'  => 'edit.php?post_type=thespl_playlist',
			'label' => __( 'Player-Builder', 'the-studio-player' ),
			'href'  => admin_url( 'edit.php?post_type=thespl_playlist' ),
		);
		$items[] = array(
			'slug'  => 'thespl-help',
			'label' => __( 'Help', 'the-studio-player' ),
			'href'  => admin_url( 'admin.php?page=thespl-help' ),
		);
	}

	$items[] = array(
		'slug'       => 'thespl-player-settings',
		'label'      => __( 'Settings', 'the-studio-player' ),
		'href'       => admin_url( 'admin.php?page=thespl-player-settings' ),
		'can_access' => thespl_current_user_can_policy( 'plugin_access' ),
		'locked_tip' => __( 'To gain access, contact your administrator.', 'the-studio-player' ),
		'locked_tag' => thespl_get_policy_locked_tag_label( 'plugin_access', __( 'Restricted', 'the-studio-player' ) ),
	);

	$items[] = array(
		'slug'       => 'thespl-tools',
		'label'      => __( 'Tools', 'the-studio-player' ),
		'href'       => admin_url( 'admin.php?page=thespl-tools' ),
		'can_access' => thespl_current_user_can_policy( 'tools_diagnose' ),
		'locked_tip' => __( 'To gain access, contact your administrator.', 'the-studio-player' ),
		'locked_tag' => thespl_get_policy_locked_tag_label( 'tools_diagnose', __( 'Restricted', 'the-studio-player' ) ),
	);

	$items[] = array(
		'slug'       => 'thespl-license',
		'label'      => __( 'License & Account', 'the-studio-player' ),
		'href'       => admin_url( 'admin.php?page=thespl-license' ),
		'can_access' => thespl_current_user_can_policy( 'license_view' ),
		'locked_tip' => __( 'To gain access, contact your administrator.', 'the-studio-player' ),
		'locked_tag' => thespl_get_policy_locked_tag_label( 'license_view', __( 'Restricted', 'the-studio-player' ) ),
	);

	$profile    = thespl_get_capability_profile();
	$brand_text = ! empty( $profile['is_pro'] ) ? 'THESPL Pro' : 'THESPL';
	$brand_text = apply_filters( 'thespl_admin_brand_label', $brand_text );
	$logo_url   = apply_filters( 'thespl_admin_brand_logo_url', '' );

	echo '<div class="nav-tab-wrapper thespl-admin-top-nav" role="tablist">';
	echo '<div class="thespl-admin-top-nav__tabs">';
	echo '<span class="thespl-admin-top-nav__brand" aria-label="' . esc_attr__( 'The Studio Player', 'the-studio-player' ) . '">';
	if ( is_string( $logo_url ) && '' !== $logo_url ) {
		echo '<img class="thespl-admin-top-nav__brand-logo" src="' . esc_url( $logo_url ) . '" alt="' . esc_attr( $brand_text ) . '" />';
	} else {
		echo esc_html( $brand_text );
	}
	echo '</span>';
	foreach ( $items as $it ) {
		$is_active  = ( $active === $it['slug'] );
		$cls        = 'nav-tab' . ( $is_active ? ' nav-tab-active' : '' );
		$can_access = ! isset( $it['can_access'] ) || ! empty( $it['can_access'] );
		if ( $can_access ) {
			echo '<a href="' . esc_url( $it['href'] ) . '" class="' . esc_attr( $cls ) . '">' . esc_html( $it['label'] ) . '</a>';
			continue;
		}

		$locked_cls = $cls . ' thespl-admin-nav-tab-disabled';
		$locked_tip = isset( $it['locked_tip'] ) ? (string) $it['locked_tip'] : __( 'To gain access, contact your administrator.', 'the-studio-player' );
		$locked_tag = isset( $it['locked_tag'] ) ? (string) $it['locked_tag'] : __( 'Restricted', 'the-studio-player' );
		echo '<span class="' . esc_attr( $locked_cls ) . '" title="' . esc_attr( $locked_tip ) . '" aria-disabled="true">';
		echo '<span class="thespl-admin-nav-tab-label">' . esc_html( $it['label'] ) . '</span>';
		echo '<span class="thespl-admin-nav-tab-lock">🔒 ' . esc_html( $locked_tag ) . '</span>';
		echo '</span>';
	}
	echo '</div>';
	echo '</div>';
}

/**
 * Render sub-navigation for the license section.
 *
 * @param string $active_slug Active subnav slug.
 * @return void
 */
function thespl_render_license_subnav( $active_slug ) {
	$active = is_string( $active_slug ) ? $active_slug : '';
	$items  = array(
		array(
			'slug'  => 'thespl-license-overview',
			'label' => __( 'Overview', 'the-studio-player' ),
			'href'  => admin_url( 'admin.php?page=thespl-license' ),
		),
		array(
			'slug'  => 'thespl-license-account',
			'label' => __( 'Account', 'the-studio-player' ),
		),
	);

	echo '<div class="nav-tab-wrapper thespl-license-subnav" role="tablist">';
	foreach ( $items as $it ) {
		$is_active = ( $active === $it['slug'] );
		$cls       = 'nav-tab' . ( $is_active ? ' nav-tab-active' : '' );
		echo '<a href="' . esc_url( $it['href'] ) . '" class="' . esc_attr( $cls ) . '">' . esc_html( $it['label'] ) . '</a>';
	}
	echo '</div>';
}

/**
 * Render top navigation on Freemius account pages.
 *
 * @return void
 */
function thespl_render_admin_top_nav_for_freemius() {
	if ( ! is_admin() ) {
		return;
	}

	$page = thespl_get_current_plugin_admin_page();

	if ( 'the-studio-player-account' !== $page ) {
		return;
	}

	$active = 'thespl-license-account';

	thespl_render_admin_top_nav( 'thespl-license' );
	thespl_render_license_subnav( $active );
}
/**
 * Add custom columns to the media library list table.
 *
 * @param array $columns Existing media list columns.
 * @return array Modified columns including attachment ID and source columns.
 */
function thespl_media_list_add_id_column( $columns ) {
	$new = array();

	foreach ( $columns as $key => $label ) {
		$new[ $key ] = $label;
		if ( 'title' === $key ) {
			$new['thespl_attachment_id'] = __( 'ID', 'the-studio-player' );
			$new['thespl_source']        = __( 'Uploaded by', 'the-studio-player' );
		}
	}

	if ( ! isset( $new['thespl_attachment_id'] ) ) {
		$new['thespl_attachment_id'] = __( 'ID', 'the-studio-player' );
	}
	if ( ! isset( $new['thespl_source'] ) ) {
		$new['thespl_source'] = __( 'Uploaded by', 'the-studio-player' );
	}

	return $new;
}

/**
 * Render custom media list columns for The Studio Player uploads.
 *
 * @param string $column_name Current column name.
 * @param int    $post_id     Attachment post ID.
 * @return void
 */
function thespl_media_list_render_id_column( $column_name, $post_id ) {
	if ( 'thespl_attachment_id' === $column_name ) {
		echo esc_html( (string) $post_id );
		return;
	}

	if ( 'thespl_source' === $column_name ) {
		$fingerprint = get_post_meta( $post_id, '_thespl_fingerprint', true );

		if ( '' !== (string) $fingerprint ) {
			echo esc_html__( 'The Studio Player', 'the-studio-player' );
			return;
		}

		$post = get_post( $post_id );
		if ( $post && isset( $post->post_author ) ) {
			$user = get_userdata( $post->post_author );
			if ( $user && isset( $user->display_name ) && '' !== $user->display_name ) {
				echo esc_html( $user->display_name );
				return;
			}
		}

		echo esc_html__( 'Generic upload', 'the-studio-player' );
	}
}

/**
 * Add source filter dropdown to the media library list screen.
 *
 * @return void
 */
function thespl_media_list_add_source_dropdown() {
	global $pagenow;

	if ( 'upload.php' !== $pagenow ) {
		return;
	}

	$current = thespl_get_media_list_source_filter();

	wp_nonce_field( 'thespl_media_list_filter', 'thespl_media_list_nonce' );
	echo '<select name="thespl_source">';
	echo '<option value="">' . esc_html__( 'All sources', 'the-studio-player' ) . '</option>';
	echo '<option value="thespl" ' . selected( $current, 'thespl', false ) . '>' . esc_html__( 'The Studio Player uploads', 'the-studio-player' ) . '</option>';
	echo '</select>';
}

/**
 * Filter media library query by The Studio Player uploads.
 *
 * @param WP_Query $query Current query instance.
 * @return void
 */
function thespl_media_list_filter_by_source( $query ) {
	if ( ! is_admin() || ! $query->is_main_query() ) {
		return;
	}

	$post_type = $query->get( 'post_type' );
	if ( 'attachment' !== $post_type && ! ( is_array( $post_type ) && in_array( 'attachment', $post_type, true ) ) ) {
		return;
	}

	$source = thespl_get_media_list_source_filter();
	if ( 'thespl' !== $source ) {
		return;
	}

	$existing_meta_query = $query->get( 'meta_query' );
	if ( ! is_array( $existing_meta_query ) ) {
		$existing_meta_query = array();
	}

	$existing_meta_query[] = array(
		'key'     => '_thespl_fingerprint',
		'compare' => 'EXISTS',
	);

	$query->set( 'meta_query', $existing_meta_query );
}

add_filter( 'manage_upload_columns', 'thespl_media_list_add_id_column' );
add_action( 'manage_media_custom_column', 'thespl_media_list_render_id_column', 10, 2 );
add_action( 'restrict_manage_posts', 'thespl_media_list_add_source_dropdown' );
add_action( 'pre_get_posts', 'thespl_media_list_filter_by_source' );
