<?php

/**
 * Plugin Name: Passwords Manager
 * Plugin URI: https://plugins.hirewebxperts.com/passwords-manager/
 * Description: Passwords Manager lets you store all your passwords in one place.
 * Version: 1.5.2
 * Requires at least: 6.3
 * Requires PHP: 7.4
 * Author: Coder426
 * Text Domain: passwords-manager
 * Domain Path: /languages
 * Author URI: https://www.hirewebxperts.com
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 */

if (!defined('ABSPATH')) {
	exit; // Exit if accessed directly.
}

/**
 * Define plugin constants.
 */
define('PWDMS_VAR', rand());
define('PWDMS_NAME', 'passwords-manager');
define('PWDMS_PLUGIN_URL', plugin_dir_url(__FILE__));
define('PWDMS_PLUGIN_DIR', dirname(__FILE__));
define('PWDMS_ASSETS', PWDMS_PLUGIN_URL . 'assets/');
define('PWDMS_IMG', PWDMS_PLUGIN_URL . 'assets/img/');
define('PWDMS_INC', PWDMS_PLUGIN_DIR . '/include/');
define('PWDMS_INC_URL', PWDMS_PLUGIN_URL . '/include/');

/**
 * Load language files for translations.
 */
add_action('init', 'pms_language_translate');
function pms_language_translate()
{
	$role = get_role('administrator'); // Adjust role as needed
	if ($role) {
		$role->add_cap('manage_custom_data');
	}
	$locale = determine_locale();
	$locale = apply_filters('plugin_locale', $locale, 'passwords-manager');
	unload_textdomain('passwords-manager');
	load_textdomain('passwords-manager', PWDMS_PLUGIN_DIR . '/languages/passwords-manager-' . $locale . '.mo');
	load_plugin_textdomain('passwords-manager', false, dirname(plugin_basename(__FILE__)) . '/languages');
}

/**
 * Create database tables on plugin activation.
 */
function pms_db_install()
{
	global $wpdb;
	// Create pms_category table
	$category_table = $wpdb->prefix . 'pms_category';
	$category_sql = "CREATE TABLE $category_table (
	id int(11) NOT NULL AUTO_INCREMENT,
	category varchar(55) NOT NULL,
	category_color varchar(255) NOT NULL DEFAULT '#bada55',
	PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	dbDelta($category_sql);

	// Create pms_passwords table
	$passwords_table = $wpdb->prefix . 'pms_passwords';
	$passwords_sql = "CREATE TABLE $passwords_table (
	pass_id int(11) NOT NULL AUTO_INCREMENT,
	user_name varchar(200) NOT NULL,
	user_email varchar(200) NOT NULL,
	user_password longtext NOT NULL,
	category_id int(11) NOT NULL,
	note text NOT NULL,
	url longtext NOT NULL,
	PRIMARY KEY (pass_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
	dbDelta($passwords_sql);
}
register_activation_hook(__FILE__, 'pms_db_install');

/**
 * Drop database tables on plugin uninstall.
 */
function delete_plugin_database_tables()
{
	global $wpdb;

	$tables = [
		$wpdb->prefix . 'pms_passwords',
		$wpdb->prefix . 'pms_category',
	];

	foreach ($tables as $table) {
		$wpdb->query("DROP TABLE IF EXISTS $table");
	}

	delete_option('pms_encrypt_key');
}
register_uninstall_hook(__FILE__, 'delete_plugin_database_tables');

/**
 * Redirect to settings after plugin activation.
 */
add_action('activated_plugin', 'pms_after_activation_redirect');
function pms_after_activation_redirect($plugin)
{
	if ($plugin === plugin_basename(__FILE__)) {
		wp_redirect(admin_url('admin.php?page=pms_menu&tab=settings'));
		exit;
	}
}

/**
 * Insert default category if none exists.
 */
add_action('init', 'pms_insert_category');
function pms_insert_category()
{
	global $wpdb;

	$category_table = $wpdb->prefix . 'pms_category';
	$category_exists = $wpdb->get_var("SELECT COUNT(*) FROM $category_table");

	if ($category_exists == 0) {
		$wpdb->insert(
			$category_table,
			['category' => 'Uncategorized', 'category_color' => '#bada55'],
			['%s', '%s']
		);
	}
}

/**
 * Add settings link to the plugins page.
 */
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'pms_add_plugin_page_settings_link');
function pms_add_plugin_page_settings_link($links)
{
	$links[] = '<a href="' . admin_url('admin.php?page=pms_menu&tab=settings') . '">' . __('Settings', 'passwords-manager') . '</a>';
	return $links;
}

/**
 * Add a live demo link to the plugin row meta.
 */
add_filter('plugin_row_meta', 'pms_live_demo_meta_links', 10, 2);
function pms_live_demo_meta_links($meta_fields, $file)
{
	if (plugin_basename(__FILE__) === $file) {
		$plugin_url = "https://youtu.be/B9NvLynWueU";
		$meta_fields[] = '<a href="' . esc_url($plugin_url) . '" target="_blank" title="' . __('Live Demo', 'passwords-manager') . '">'
			. __('Live Demo', 'passwords-manager') . '</a>';
	}
	return $meta_fields;
}


/*
**include script & style file
*/
include(PWDMS_INC . 'pms-srcipts-styles.php');
/*
**include encryption file
*/
include(PWDMS_INC . 'pms-functions.php');
/*
**include frontend shortcode file
*/
include(PWDMS_INC . 'pms-front-shortcode.php');
/*
**include encryption file
*/
include(PWDMS_INC . 'pms-encryption.php');
/*
**include category action file
*/
include(PWDMS_INC . 'pms-categories-ajax-action.php');
/*
**include pass action file
*/
include(PWDMS_INC . 'pms-passwords-ajax-action.php');
/*
**include Setting action file
*/
include(PWDMS_INC . 'pms-settings-ajax-action.php');

include(PWDMS_INC . 'admin-page/addon/csv-export/index.php');
