<?php
/*
Plugin Name: QuickLinks Manager by Press.Zone
Plugin URI: https://press.zone/plugins/quicklinks-manager
Description: Manage custom quick links in WordPress dashboard.
Version: 2.1.2
Author: Avi Ezra
Author URI: https://www.press.zone
License: GPLv2 or later
Text Domain: quicklinks-manager
*/

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

/**
 * Get default plugin options.
 */
function quicklinks_manager_get_default_options() {
	return array(
		'resite'             => 'QuickLinks Manager by Press.Zone',
		'quicklinks_manager' => array(),
		'allowed_roles'      => array( 'administrator', 'editor' ),
	);
}

/**
 * Sanitize a single link array.
 */
function quicklinks_manager_sanitize_link( $link ) {
	return array(
		'rs_pz_title'  => isset( $link['rs_pz_title'] ) ? sanitize_text_field( $link['rs_pz_title'] ) : '',
		'rs_pz_url'    => isset( $link['rs_pz_url'] ) ? esc_url_raw( $link['rs_pz_url'] ) : '',
		'rs_pz_newtab' => isset( $link['rs_pz_newtab'] ) ? absint( $link['rs_pz_newtab'] ) : 0,
	);
}

/**
 * Plugin activation check.
 */
function quicklinks_manager_check() {
	$opts = get_option( 'quicklinks_manager_options' );
	if ( false === $opts ) {
		update_option( 'quicklinks_manager_options', maybe_serialize( quicklinks_manager_get_default_options() ) );
	}
	add_action( 'admin_notices', 'quicklinks_manager_admin_notice' );
}
add_action( 'activate_quicklinks-manager/plugin.php', 'quicklinks_manager_check' );

/**
 * Enqueue admin scripts.
 */
function quicklinks_manager_scripts( $hook ) {
	if ( 'tools_page_quicklinks-manager-settings-pz' !== $hook ) {
		return;
	}
	wp_enqueue_script( 'jquery-ui-core' );
	wp_enqueue_script( 'jquery-ui-tabs' );
	wp_enqueue_script( 'jquery-ui-sortable' );
	wp_enqueue_script( 'quicklinks-manager-scripts', plugins_url( 'assets/js/scripts.js', __FILE__ ), array( 'jquery', 'jquery-ui-core', 'jquery-ui-tabs', 'jquery-ui-sortable' ), '2.1.1', true );
	wp_localize_script( 'quicklinks-manager-scripts', 'quickLinksManager', array(
		'ajax_url' => admin_url( 'admin-ajax.php' ),
		'nonce'    => wp_create_nonce( 'quicklinks_manager_nonce' ),
	));
}
add_action( 'admin_enqueue_scripts', 'quicklinks_manager_scripts' );

/**
 * Enqueue admin styles.
 */
function quicklinks_manager_load_custom_wp_admin_style() {
	wp_register_style( 'quicklinks-manager-css', plugins_url( 'assets/css/style.css', __FILE__ ), false, '2.1.1' );
	wp_enqueue_style( 'quicklinks-manager-css' );
}
add_action( 'admin_enqueue_scripts', 'quicklinks_manager_load_custom_wp_admin_style' );

/**
 * Admin activation notice.
 */
function quicklinks_manager_admin_notice() {
	?>
	<div class="notice notice-success is-dismissible rs-pz-close">
		<p><?php echo esc_html__( 'QuickLinks Manager by Press.Zone is now active! Go to ', 'quicklinks-manager' ); ?><a href="<?php echo esc_url( admin_url( 'tools.php?page=quicklinks-manager-settings-pz' ) ); ?>"><?php esc_html_e( 'Setting Page', 'quicklinks-manager' ); ?></a></p>
	</div>
	<?php
}

/**
 * Dismiss admin notice via AJAX.
 */
function quicklinks_manager_dismiss_notice() {
	remove_action( 'admin_notices', 'quicklinks_manager_admin_notice' );
	wp_send_json_success();
}
add_action( 'wp_ajax_quicklinks_manager_dismiss_notice', 'quicklinks_manager_dismiss_notice' );

/**
 * Add tools submenu page.
 */
function quicklinks_manager_add_tools_submenu() {
	add_submenu_page(
		'tools.php',
		esc_html__( 'QuickLinks Manager by Press.Zone', 'quicklinks-manager' ),
		esc_html__( 'QuickLinks Manager', 'quicklinks-manager' ),
		'manage_options',
		'quicklinks-manager-settings-pz',
		'quicklinks_manager_render_settings_page'
	);
}
add_action( 'admin_menu', 'quicklinks_manager_add_tools_submenu' );

/**
 * Add plugin action links.
 */
function quicklinks_manager_add_plugin_action_links( $links ) {
	$settings_link = '<a href="tools.php?page=quicklinks-manager-settings-pz">' . esc_html__( 'Settings', 'quicklinks-manager' ) . '</a>';
	array_unshift( $links, $settings_link );

	$resite_link = '<a href="https://resite.pro" target="_blank">' . esc_html__( 'RESITE', 'quicklinks-manager' ) . '</a>';
	array_unshift( $links, $resite_link );

	return $links;
}
$plugin_basename = plugin_basename( __FILE__ );
add_filter( "plugin_action_links_$plugin_basename", 'quicklinks_manager_add_plugin_action_links' );

/**
 * AJAX: Save links.
 */
function quicklinks_manager_save() {
	// Security check
	if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'quicklinks_manager_nonce' ) ) {
		wp_send_json_error( 'Security check failed' );
	}
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( 'Permission denied' );
	}

	// Get and sanitize links
	$raw_links = isset( $_POST['links'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['links'] ) ), true ) : array();
	$sanitized_links = array();
	if ( is_array( $raw_links ) ) {
		foreach ( $raw_links as $link ) {
			$sanitized_links[] = quicklinks_manager_sanitize_link( $link );
		}
	}

	// Get and sanitize allowed roles
	$raw_roles = isset( $_POST['allowed_roles'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['allowed_roles'] ) ), true ) : array( 'administrator', 'editor' );
	$allowed_roles = array();
	if ( is_array( $raw_roles ) ) {
		foreach ( $raw_roles as $role ) {
			$allowed_roles[] = sanitize_text_field( $role );
		}
	}

	// Save with the CORRECT database key
	$new_arr = array(
		'resite'             => 'QuickLinks Manager by Press.Zone',
		'quicklinks_manager' => $sanitized_links,
		'allowed_roles'      => $allowed_roles,
	);
	update_option( 'quicklinks_manager_options', maybe_serialize( $new_arr ) );
	wp_send_json_success( 'Links saved successfully' );
}
add_action( 'wp_ajax_quicklinks_manager_save', 'quicklinks_manager_save' );

/**
 * AJAX: Update link order (drag & drop).
 */
function quicklinks_manager_update_order() {
	// Security check
	if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'quicklinks_manager_nonce' ) ) {
		wp_send_json_error( 'Security check failed' );
	}
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( 'Permission denied' );
	}

	// Get and sanitize links
	$raw_links = isset( $_POST['links'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['links'] ) ), true ) : array();
	$sanitized_links = array();
	if ( is_array( $raw_links ) ) {
		foreach ( $raw_links as $link ) {
			$sanitized_links[] = quicklinks_manager_sanitize_link( $link );
		}
	}

	// Get existing options to preserve allowed_roles
	$opts = get_option( 'quicklinks_manager_options' );
	$existing = maybe_unserialize( $opts );
	$allowed_roles = isset( $existing['allowed_roles'] ) ? $existing['allowed_roles'] : array( 'administrator', 'editor' );

	$new_arr = array(
		'resite'             => 'QuickLinks Manager by Press.Zone',
		'quicklinks_manager' => $sanitized_links,
		'allowed_roles'      => $allowed_roles,
	);
	update_option( 'quicklinks_manager_options', maybe_serialize( $new_arr ) );
	wp_send_json_success( 'Order updated' );
}
add_action( 'wp_ajax_quicklinks_manager_update_order', 'quicklinks_manager_update_order' );

/**
 * Render settings page with modern UI.
 */
function quicklinks_manager_render_settings_page() {
	$opts = get_option( 'quicklinks_manager_options' );
	$unserialized = maybe_unserialize( $opts );

	if ( ! $unserialized || ! isset( $unserialized['resite'] ) || 'QuickLinks Manager by Press.Zone' !== $unserialized['resite'] ) {
		$unserialized = quicklinks_manager_get_default_options();
		update_option( 'quicklinks_manager_options', maybe_serialize( $unserialized ) );
	}

	$arr_links     = isset( $unserialized['quicklinks_manager'] ) ? $unserialized['quicklinks_manager'] : array();
	$allowed_roles = isset( $unserialized['allowed_roles'] ) ? $unserialized['allowed_roles'] : array( 'administrator', 'editor' );

	// Get all editable roles
	$editable_roles = get_editable_roles();
	?>
	<div class="wrap">
		<h1><?php esc_html_e( 'QuickLinks Manager Settings', 'quicklinks-manager' ); ?></h1>

		<div id="rs-pz-tabs">
			<ul class="nav-tab-wrapper">
				<li><a class="nav-tab" href="#general"><?php esc_html_e( 'General', 'quicklinks-manager' ); ?></a></li>
				<li><a class="nav-tab" href="#import-export"><?php esc_html_e( 'Import / Export', 'quicklinks-manager' ); ?></a></li>
			</ul>

			<div class="tabs-content">
				<!-- GENERAL TAB -->
				<div id="general">
					<p class="qlm-description"><?php esc_html_e( 'Add, edit and remove custom links to display in your WordPress dashboard.', 'quicklinks-manager' ); ?></p>

					<!-- ADD NEW LINK CARD -->
					<div class="qlm-add-card">
						<h3 class="qlm-add-card__title">
							<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="16"/><line x1="8" y1="12" x2="16" y2="12"/></svg>
							<?php esc_html_e( 'Add New Link', 'quicklinks-manager' ); ?>
						</h3>
						<div class="qlm-add-card__form">
							<div class="qlm-add-card__field">
								<label for="rs-pz-name"><?php esc_html_e( 'Display Text', 'quicklinks-manager' ); ?></label>
								<input type="text" id="rs-pz-name" placeholder="<?php esc_attr_e( 'Link Title', 'quicklinks-manager' ); ?>">
							</div>
							<div class="qlm-add-card__field qlm-add-card__field--url">
								<label for="rs-pz-url"><?php esc_html_e( 'URL', 'quicklinks-manager' ); ?></label>
								<input type="text" id="rs-pz-url" placeholder="https://example.com">
							</div>
							<div class="qlm-add-card__field qlm-add-card__field--checkbox">
								<label>
									<input type="checkbox" id="rs-pz-new-tab">
									<span><?php esc_html_e( 'New Tab', 'quicklinks-manager' ); ?></span>
								</label>
							</div>
							<button type="button" class="qlm-add-btn rs-pz-add-link">
								<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
								<?php esc_html_e( 'Add Link', 'quicklinks-manager' ); ?>
							</button>
						</div>
					</div>

					<!-- LINKS TABLE -->
					<div class="qlm-table-wrapper">
						<table class="qlm-table">
							<thead>
								<tr>
									<th class="qlm-col-drag"></th>
									<th class="qlm-col-title"><?php esc_html_e( 'Display Text', 'quicklinks-manager' ); ?></th>
									<th class="qlm-col-url"><?php esc_html_e( 'URL', 'quicklinks-manager' ); ?></th>
									<th class="qlm-col-newtab"><?php esc_html_e( 'New Tab', 'quicklinks-manager' ); ?></th>
									<th class="qlm-col-actions"><?php esc_html_e( 'Actions', 'quicklinks-manager' ); ?></th>
								</tr>
							</thead>
							<tbody id="qlm-links-body">
								<?php if ( ! empty( $arr_links ) ) : ?>
									<?php foreach ( $arr_links as $index => $link ) : ?>
										<tr data-index="<?php echo esc_attr( $index ); ?>">
											<td class="qlm-col-drag">
												<span class="qlm-drag-handle" title="<?php esc_attr_e( 'Drag to reorder', 'quicklinks-manager' ); ?>">
													<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"><circle cx="9" cy="6" r="1.5"/><circle cx="15" cy="6" r="1.5"/><circle cx="9" cy="12" r="1.5"/><circle cx="15" cy="12" r="1.5"/><circle cx="9" cy="18" r="1.5"/><circle cx="15" cy="18" r="1.5"/></svg>
												</span>
											</td>
											<td class="qlm-col-title">
												<input type="text" class="rs-pz-name qlm-input" value="<?php echo esc_attr( $link['rs_pz_title'] ); ?>">
											</td>
											<td class="qlm-col-url">
												<input type="text" class="rs-pz-url qlm-input" value="<?php echo esc_url( $link['rs_pz_url'] ); ?>">
											</td>
											<td class="qlm-col-newtab">
												<label class="qlm-toggle">
													<input type="checkbox" value="1" <?php checked( 1, $link['rs_pz_newtab'] ); ?>>
													<span class="qlm-toggle__slider"></span>
												</label>
											</td>
											<td class="qlm-col-actions">
												<div class="qlm-actions">
													<button type="button" class="qlm-btn qlm-move-up" title="<?php esc_attr_e( 'Move up', 'quicklinks-manager' ); ?>">
														<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="18 15 12 9 6 15"/></svg>
													</button>
													<button type="button" class="qlm-btn qlm-move-down" title="<?php esc_attr_e( 'Move down', 'quicklinks-manager' ); ?>">
														<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="6 9 12 15 18 9"/></svg>
													</button>
													<button type="button" class="qlm-btn qlm-btn--delete rs-pz-remove-link" title="<?php esc_attr_e( 'Delete', 'quicklinks-manager' ); ?>">
														<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
													</button>
												</div>
											</td>
										</tr>
									<?php endforeach; ?>
								<?php endif; ?>
							</tbody>
						</table>
						<div id="qlm-empty-state" class="qlm-empty-state" <?php echo ! empty( $arr_links ) ? 'style="display:none;"' : ''; ?>>
							<svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>
							<p><?php esc_html_e( 'No links yet. Add your first link above!', 'quicklinks-manager' ); ?></p>
						</div>
					</div>

					<!-- ROLES CARD -->
					<div class="qlm-roles-card">
						<h3 class="qlm-roles-card__title">
							<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/></svg>
							<?php esc_html_e( 'Admin Bar Visibility', 'quicklinks-manager' ); ?>
						</h3>
						<p class="qlm-roles-card__description"><?php esc_html_e( 'Select which user roles can see QuickLinks in the Admin Bar.', 'quicklinks-manager' ); ?></p>
						<div id="qlm-roles-grid" class="qlm-roles-grid">
							<?php foreach ( $editable_roles as $role_slug => $role_info ) : ?>
								<label class="qlm-role-checkbox">
									<input type="checkbox" name="quicklinks_manager_allowed_roles[]" value="<?php echo esc_attr( $role_slug ); ?>" <?php checked( in_array( $role_slug, $allowed_roles, true ) ); ?>>
									<span class="qlm-role-checkbox__label"><?php echo esc_html( translate_user_role( $role_info['name'] ) ); ?></span>
								</label>
							<?php endforeach; ?>
						</div>
					</div>

					<!-- SAVE BUTTON -->
					<div class="qlm-save-wrapper">
						<button type="button" class="qlm-save-btn" id="rs-pz-update-links">
							<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/><polyline points="17 21 17 13 7 13 7 21"/><polyline points="7 3 7 8 15 8"/></svg>
							<?php esc_html_e( 'Save Links', 'quicklinks-manager' ); ?>
						</button>
						<span id="qlm-save-status" class="qlm-save-status"></span>
					</div>
				</div>

				<!-- IMPORT/EXPORT TAB -->
				<div id="import-export">
					<p><?php esc_html_e( 'Export your links to a JSON file or import links from a JSON file.', 'quicklinks-manager' ); ?></p>
					<p>
						<input type="file" id="rs-pz-import-links" accept=".json" />
						<button id="rs-pz-import-links-btn" class="button-primary"><?php esc_html_e( 'Import Links', 'quicklinks-manager' ); ?></button>
					</p>
					<p>
						<button id="rs-pz-export-data" class="button-primary"><?php esc_html_e( 'Export Links', 'quicklinks-manager' ); ?></button>
					</p>
				</div>
			</div>
		</div>
	</div>
	<?php
}

/**
 * AJAX: Import handler.
 */
function quicklinks_manager_import_handler() {
	if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'quicklinks_manager_nonce' ) ) {
		wp_send_json_error( 'Security check failed' );
	}
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_send_json_error( 'No permission' );
	}

	if ( ! isset( $_FILES['file'] ) || ! isset( $_FILES['file']['tmp_name'] ) ) {
		wp_send_json_error( 'No file uploaded' );
	}

	// Read file content
	$file_content = file_get_contents( $_FILES['file']['tmp_name'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
	if ( false === $file_content ) {
		wp_send_json_error( 'Failed to read file' );
	}

	// Try Base64 decode first (for backward compatibility)
	$decoded = base64_decode( $file_content, true );
	if ( false !== $decoded ) {
		$arr_links = json_decode( $decoded, true );
	} else {
		// Try direct JSON
		$arr_links = json_decode( $file_content, true );
	}

	if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $arr_links ) ) {
		wp_send_json_error( 'Invalid JSON format' );
	}

	// Sanitize imported links
	$sanitized_links = array();
	foreach ( $arr_links as $link ) {
		$sanitized_links[] = quicklinks_manager_sanitize_link( $link );
	}

	// Merge with existing
	$opts = get_option( 'quicklinks_manager_options' );
	$existing = maybe_unserialize( $opts );
	$existing_links = isset( $existing['quicklinks_manager'] ) ? $existing['quicklinks_manager'] : array();
	$allowed_roles = isset( $existing['allowed_roles'] ) ? $existing['allowed_roles'] : array( 'administrator', 'editor' );

	$merged_links = array_merge( $existing_links, $sanitized_links );

	$new_value = array(
		'resite'             => 'QuickLinks Manager by Press.Zone',
		'quicklinks_manager' => $merged_links,
		'allowed_roles'      => $allowed_roles,
	);
	update_option( 'quicklinks_manager_options', maybe_serialize( $new_value ) );

	wp_send_json_success( 'Imported successfully' );
}
add_action( 'wp_ajax_quicklinks_manager_import', 'quicklinks_manager_import_handler' );

/**
 * AJAX: Export handler.
 */
function quicklinks_manager_export_handler() {
	if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'quicklinks_manager_nonce' ) ) {
		wp_send_json_error( 'Security check failed' );
	}

	$opts = get_option( 'quicklinks_manager_options' );
	$unserialized = maybe_unserialize( $opts );

	if ( ! $unserialized || ! isset( $unserialized['quicklinks_manager'] ) ) {
		wp_send_json_error( 'No links to export' );
	}

	$arr_links = $unserialized['quicklinks_manager'];
	// Return Base64 encoded JSON for backward compatibility
	echo base64_encode( wp_json_encode( $arr_links ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	wp_die();
}
add_action( 'wp_ajax_quicklinks_manager_export', 'quicklinks_manager_export_handler' );

/**
 * Add dashboard widget.
 */
function quicklinks_manager_add_dashboard_widgets() {
	$opts = get_option( 'quicklinks_manager_options' );
	$unserialized = maybe_unserialize( $opts );
	$allowed_roles = isset( $unserialized['allowed_roles'] ) ? $unserialized['allowed_roles'] : array( 'administrator', 'editor' );

	$user = wp_get_current_user();
	$user_roles = $user->roles;
	$can_view = array_intersect( $user_roles, $allowed_roles );

	if ( ! empty( $can_view ) ) {
		wp_add_dashboard_widget( 'pz_quicklinks_dashboard_widget', esc_html__( 'Quick Links', 'quicklinks-manager' ), 'quicklinks_manager_dashboard_widget_function' );
	}
}
add_action( 'wp_dashboard_setup', 'quicklinks_manager_add_dashboard_widgets' );

/**
 * Render dashboard widget.
 */
function quicklinks_manager_dashboard_widget_function() {
	$opts = get_option( 'quicklinks_manager_options' );
	$unserialized = maybe_unserialize( $opts );

	if ( $unserialized && 'QuickLinks Manager by Press.Zone' === $unserialized['resite'] ) {
		$arr_links = isset( $unserialized['quicklinks_manager'] ) ? $unserialized['quicklinks_manager'] : array();
		?>
		<div class="pz-widget-links">
			<?php if ( ! empty( $arr_links ) ) : ?>
				<ul class="rs-pz-quicklinks">
					<?php foreach ( $arr_links as $link ) : ?>
						<li>
							<a href="<?php echo esc_url( $link['rs_pz_url'] ); ?>" target="<?php echo $link['rs_pz_newtab'] ? '_blank' : '_self'; ?>"><?php echo esc_html( $link['rs_pz_title'] ); ?></a>
						</li>
					<?php endforeach; ?>
				</ul>
			<?php else : ?>
				<p><?php esc_html_e( 'No quick links configured.', 'quicklinks-manager' ); ?></p>
			<?php endif; ?>
			<div class="rtl"><a href="<?php echo esc_url( admin_url( 'tools.php?page=quicklinks-manager-settings-pz' ) ); ?>"><?php esc_html_e( '🔗 Manage Links', 'quicklinks-manager' ); ?></a></div>
			<div class="footer-credits">
				<p>
					<?php esc_html_e( 'QuickLinks Manager by', 'quicklinks-manager' ); ?>
					<a href="https://press.zone" target="_blank" rel="noopener noreferrer">Press.Zone</a> &amp;
					<a href="mailto:admin@press.zone">Avi Ezra</a> &amp;
					<a href="https://resite.pro" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'RESITE custom WP', 'quicklinks-manager' ); ?></a>
				</p>
			</div>
		</div>
		<?php
	} else {
		echo '<p>' . esc_html__( 'Error loading links.', 'quicklinks-manager' ) . '</p>';
	}
}

/**
 * Add links to admin bar.
 */
function quicklinks_manager_add_links_to_admin_bar( $wp_admin_bar ) {
	if ( ! is_admin_bar_showing() ) {
		return;
	}

	$opts = get_option( 'quicklinks_manager_options' );
	$unserialized = maybe_unserialize( $opts );

	// Check role-based access
	$allowed_roles = isset( $unserialized['allowed_roles'] ) ? $unserialized['allowed_roles'] : array( 'administrator', 'editor' );
	$user = wp_get_current_user();
	$user_roles = $user->roles;
	$can_view = array_intersect( $user_roles, $allowed_roles );

	if ( empty( $can_view ) ) {
		return;
	}

	// Add parent node
	$wp_admin_bar->add_node( array(
		'id'    => 'pz_quicklinks',
		'title' => esc_html__( 'QuickLinks', 'quicklinks-manager' ),
		'href'  => false,
	));

	// Add child nodes
	if ( $unserialized && isset( $unserialized['quicklinks_manager'] ) && ! empty( $unserialized['quicklinks_manager'] ) ) {
		foreach ( $unserialized['quicklinks_manager'] as $index => $link ) {
			$wp_admin_bar->add_node( array(
				'parent' => 'pz_quicklinks',
				'id'     => 'pz_quicklink_' . $index,
				'title'  => esc_html( $link['rs_pz_title'] ),
				'href'   => esc_url( $link['rs_pz_url'] ),
				'meta'   => array(
					'target' => $link['rs_pz_newtab'] ? '_blank' : '_self',
				),
			));
		}
	}

	// Add "Manage QuickLinks" link at the bottom
	$wp_admin_bar->add_node( array(
		'parent' => 'pz_quicklinks',
		'id'     => 'pz_quicklinks_manage',
		'title'  => '🔗 ' . esc_html__( 'Manage QuickLinks', 'quicklinks-manager' ),
		'href'   => admin_url( 'tools.php?page=quicklinks-manager-settings-pz' ),
	));
}
add_action( 'admin_bar_menu', 'quicklinks_manager_add_links_to_admin_bar', 100 );
