<?php  
// checks for version and automatically upgrades to latest

class check_update {

	public $version;

	public function __construct($app) {
		$this->app = $app;
		$this->version = VERSION;
		require_once(ABSPATH."contents/plugins/plugins.php");
		require_once(ABSPATH."contents/themes/theme.php");
		$this->plugins = new plugins();
		$this->theme = new theme($this->app);
	}

	public function run() {
		// if($this->version >= 1) {
			set_time_limit(200);
			// emptyDir(ABSPATH.BUNDLEPATH);
			$common_access = ['dashboard_menu', 'settings_menu', 'profile', 'logout', 'login', 'error404', 'registration', 'activation', 'forgot_password', 'otp', 'change_password', 'search_results'];
			add_common_access($common_access);
			$super_admin = ['sa_only', "users", "see_from_super_admin", 'edit_others_profile', 'edit_own_profile', 'change_user_role', 'change_db_section', "delete_user", 'uploads', 'view_upload_library', 'add_user', "deactivate_user", "activate_user", "standalone_upload", "delete_post", "deactivate_post", "activate_post", "edit_post"]; //content owner
			$super_admin_tech = ['sat_only', "users", "see_from_super_admin_tech", 'software_set_up', 'edit_others_profile', 'edit_own_profile', 'change_user_role', 'change_db_section', 'login_as_user', 'edit_pages', 'menu', 'edit_roles', 'edit_forms', "delete_user", 'uploads', 'view_upload_library', 'add_user', "deactivate_user", "activate_user", "standalone_upload", 'add_db_section', "edit_db_section", "delete_post", "deactivate_post", "activate_post", "visitors", "external_visits", "site_user_binding", "approve_inward_site_binding", "edit_post"]; //technical owner
			$admin = ['admin_only', "users", "see_from_admin", 'edit_others_profile', 'edit_own_profile', 'uploads', 'view_upload_library', 'add_user', "deactivate_user", "activate_user", "standalone_upload", "delete_post", "deactivate_post", "activate_post", "edit_post"]; //content owner
			$admin_tech = ['at_only', "users", "see_from_admin_tech", 'edit_others_profile', 'edit_own_profile', 'uploads', 'view_upload_library', 'add_user', "deactivate_user", "activate_user", "standalone_upload", "delete_post", "deactivate_post", "activate_post", "edit_post"]; //technical owner
			$member = ['member_only', 'edit_own_profile', 'uploads', 'view_upload_library'];
			$pending = ['pending_only', 'edit_own_profile'];
			$unverified = ['unverified_only', 'edit_own_profile'];
			$se = ['se_only', "see_from_software_engineers", 'update_software', 'software_set_up', 'plugins_settings', 'custom_css', 'cron_tabs', 'cron_logs'];
			
			update_role('super_admin', $super_admin);
			update_role('admin', $admin);
			update_role('super_admin_tech', $super_admin_tech);
			update_role('admin_tech', $admin_tech);
			update_role('member', $member);
			update_role('pending', $pending);
			update_role('unverified', $unverified);
			unset($super_admin[0]); 
			unset($super_admin_tech[0]); 
			unset($admin[0]); 
			unset($admin_tech[0]); 
			unset($member[0]);
			unset($pending[0]);
			unset($unverified[0]);
			update_role('software_engineer', array_unique(array_merge($super_admin, $admin, $super_admin_tech, $admin_tech, $member, $se, $unverified, $pending)));
			// die();
			// creating default pages
			$post = new post;
			register_admin_page(array(
				'title' => 'Dashboard',
				'callback' => 'dashboard_cb',
				'access' => 'dashboard_menu',
				'permalink' => 'dashboard'
			));
			register_admin_page(array(
				'title' => 'Software Set Up',
				'callback' => 'software_set_up_cb',
				'access' => 'software_set_up',
				'permalink' => 'software-set-up'
			));
			register_admin_page(array(
				'title' => 'Plugins Settings',
				'callback' => 'plugins_settings_cb',
				'access' => 'plugins_settings',
				'permalink' => 'plugins-settings'
			));
			register_admin_page(array(
				'title' => 'Database Section',
				'callback' => 'add_db_section_cb',
				'access' => 'add_db_section',
				'permalink' => 'add-db-section'
			));
			register_admin_page(array(
				'title' => 'Edit Database Section',
				'callback' => 'edit_db_section_cb',
				'access' => 'edit_db_section',
				'permalink' => 'edit-db-section'
			));
			register_admin_page(array(
				'title' => 'User Profile',
				'callback' => 'profile_cb',
				'access' => 'profile',
				'permalink' => 'profile'
			));
			register_admin_page(array(
				'title' => 'Logout',
				'callback' => 'logout_cb',
				'access' => 'logout',
				'permalink' => 'logout',
				'availability' => 'public',
				'template' => 'login'
			));
			register_admin_page(array(
				'title' => 'Menu Settings',
				'callback' => 'menu_cb',
				'access' => 'menu',
				'permalink' => 'menu'
			));
			register_admin_page(array(
				'title' => 'Edit Pages',
				'callback' => 'edit_pages_cb',
				'access' => 'edit_pages',
				'permalink' => 'edit-pages'
			));
			register_admin_page(array(
				'title' => 'Edit Roles',
				'callback' => 'edit_roles_cb',
				'access' => 'edit_roles',
				'permalink' => 'edit-roles'
			));
			register_admin_page(array(
				'title' => 'Users',
				'callback' => 'users_cb',
				'access' => 'users',
				'permalink' => 'users'
			));
			register_admin_page(array(
				'title' => 'Login',
				'callback' => 'login_cb',
				'access' => 'login',
				'permalink' => 'login',
				'availability' => 'public',
				'template' => 'login'
			));
			register_admin_page(array(
				'title' => 'Error 404',
				'callback' => 'error404_cb',
				'access' => 'error404',
				'permalink' => 'error404',
				'availability' => 'public'
			));
			register_admin_page(array(
				'title' => 'Registration',
				'callback' => 'registration_cb',
				'access' => 'registration',
				'permalink' => 'registration',
				'availability' => 'public',
				'template' => 'login'
			));
			register_admin_page(array(
				'title' => 'activation',
				'callback' => 'activation_cb',
				'access' => 'activation',
				'permalink' => 'activation',
				'availability' => 'public',
				'template' => 'login'
			));
			register_admin_page(array(
				'title' => 'Password Recovery',
				'callback' => 'forgot_password_cb',
				'access' => 'forgot_password',
				'permalink' => 'forgot_password',
				'availability' => 'public',
				'template' => 'login'
			));
			register_admin_page(array(
				'title' => 'OTP',
				'callback' => 'otp_cb',
				'access' => 'otp',
				'permalink' => 'otp',
				'availability' => 'public'
			));
			register_admin_page(array(
				'title' => 'Edit Forms',
				'callback' => 'edit_forms_cb',
				'access' => 'edit_forms',
				'permalink' => 'edit-forms'
			));
			register_admin_page(array(
				'title' => 'Uploads',
				'callback' => 'uploads_cb',
				'access' => 'uploads',
				'permalink' => 'uploads-libraries'
			));
			register_admin_page(array(
				'title' => 'View Upload Library',
				'callback' => 'view_upload_library_cb',
				'access' => 'view_upload_library',
				'permalink' => 'view-upload-library'
			));
			register_admin_page(array(
				'title' => 'Add New User',
				'callback' => 'add_user_cb',
				'access' => 'add_user',
				'permalink' => 'add-user'
			));
			register_admin_page(array(
				'title' => 'Standalone Upload',
				'callback' => 'standalone_upload_cb',
				'access' => 'standalone_upload',
				'permalink' => 'standalone-upload'
			));
			register_admin_page(array(
				'title' => 'Visitors',
				'callback' => 'visitors_cb',
				'access' => 'visitors',
				'permalink' => 'visitors'
			));
			register_admin_page(array(
				'title' => 'External Visits',
				'callback' => 'external_visits_cb',
				'access' => 'external_visits',
				'permalink' => 'external-visits'
			));
			register_admin_page(array(
				'title' => 'Search Results',
				'callback' => 'search_results_cb',
				'access' => 'search_results',
				'permalink' => 'search-results',
				'availability' => 'public'
			));
			register_admin_page(array(
				'title' => 'Site User Binding',
				'callback' => 'site_user_binding_cb',
				'access' => 'site_user_binding',
				'permalink' => 'site-user-binding'
			));
			register_admin_page(array(
				'title' => 'Custom CSS',
				'callback' => 'custom_css_cb',
				'access' => 'custom_css',
				'permalink' => 'custom-css'
			));
			register_admin_page(array(
				'title' => 'Cron tabs',
				'callback' => 'cron_tabs_cb',
				'access' => 'cron_tabs',
				'permalink' => 'cron-tabs'
			));
			register_admin_page(array(
				'title' => 'Cron logs',
				'callback' => 'cron_logs_cb',
				'access' => 'cron_logs',
				'permalink' => 'cron-logs'
			));



			//admin menu
			
			register_admin_menu(array(
				'id' => 'dashboard',
				'title' => 'Dashboard',
				'icon' => 'fas fa-tachometer-alt',
				'page' => 'dashboard',
				'access' => 'dashboard_menu',
				'order' => 1
			));

			register_admin_menu(array(
				'id' => 'settings',
				'title' => 'Settings',
				'icon' => 'fas fa-cog',
				'page' => '',
				'access' => 'settings_menu'
			));

			register_admin_submenu(array(
				'mm_id' => 'settings',
				'id' => 'software_set_up',
				'title' => 'Software Set Up',
				'icon' => '',
				'page' => 'software-set-up',
				'access' => 'software_set_up'
			));

			register_admin_submenu(array(
				'mm_id' => 'settings',
				'id' => 'uploads',
				'title' => 'Uploads',
				'icon' => '',
				'page' => 'uploads-libraries',
				'access' => 'uploads'
			));

			// register_admin_submenu(array(
			// 	'mm_id' => 'settings',
			// 	'id' => 'standalone_upload',
			// 	'title' => 'Standalone Upload',
			// 	'icon' => '',
			// 	'page' => 'standalone-upload',
			// 	'access' => 'standalone_upload'
			// ));

			register_admin_submenu(array(
				'mm_id' => 'settings',
				'id' => 'add_db_section',
				'title' => 'Add Database Section',
				'icon' => '',
				'page' => 'add-db-section',
				'access' => 'add_db_section',
				'condition' => get_option("section_database")
			));

			register_admin_submenu(array(
				'mm_id' => 'settings',
				'id' => 'change_password',
				'title' => 'Change Password',
				'icon' => '',
				'page' => 'change-password',
				'access' => 'change_password'
			));

			register_admin_submenu(array(
				'mm_id' => 'settings',
				'id' => 'plugins_settings',
				'title' => 'Plugins Settings',
				'icon' => '',
				'page' => 'plugins-settings',
				'access' => 'plugins_settings'
			));

			register_admin_submenu(array(
				'mm_id' => 'settings',
				'id' => 'menu_settings',
				'title' => 'Menu Settings',
				'icon' => '',
				'page' => 'menu',
				'access' => 'menu'
			));

			register_admin_submenu(array(
				'mm_id' => 'settings',
				'id' => 'edit_pages',
				'title' => 'Edit Pages',
				'icon' => '',
				'page' => 'edit-pages',
				'access' => 'edit_pages'
			));

			register_admin_submenu(array(
				'mm_id' => 'settings',
				'id' => 'edit_roles',
				'title' => 'Edit Roles',
				'icon' => '',
				'page' => 'edit-roles',
				'access' => 'edit_roles'
			));

			register_admin_submenu(array(
				'mm_id' => 'settings',
				'id' => 'edit_forms',
				'title' => 'Edit Forms',
				'icon' => '',
				'page' => 'edit-forms',
				'access' => 'edit_forms'
			));

			register_admin_menu(array(
				'id' => 'users',
				'title' => 'Users',
				'icon' => 'fas fa-users',
				'page' => '',
				'access' => 'users',
				'order' => 2
			));

			register_admin_submenu(array(
				'mm_id' => 'users',
				'id' => 'users',
				'title' => 'Users',
				'icon' => '',
				'page' => 'users',
				'access' => 'users'
			));

			register_admin_submenu(array(
				'mm_id' => 'users',
				'id' => 'add_user',
				'title' => 'Add New User',
				'icon' => '',
				'page' => 'add-user',
				'access' => 'add_user'
			));

			register_admin_submenu(array(
				'mm_id' => 'users',
				'id' => 'site_user_binding',
				'title' => 'Site User Binding',
				'icon' => '',
				'page' => 'site-user-binding',
				'access' => 'site_user_binding'
			));

			register_admin_menu(array(
				'id' => 'visitors',
				'title' => 'Visitors',
				'icon' => 'far fa-eye',
				'page' => '',
				'access' => 'visitors'
			));

			register_admin_submenu(array(
				'mm_id' => 'visitors',
				'id' => 'visitors',
				'title' => 'Visitors',
				'icon' => '',
				'page' => 'visitors',
				'access' => 'visitors'
			));

			register_admin_submenu(array(
				'mm_id' => 'visitors',
				'id' => 'external-visits',
				'title' => 'External Visits',
				'icon' => '',
				'page' => 'external-visits',
				'access' => 'external_visits'
			));

			register_admin_submenu(array(
				'mm_id' => 'visitors',
				'id' => 'cron_tabs',
				'title' => 'Cron tabs',
				'icon' => '',
				'page' => 'cron-tabs',
				'access' => 'cron_tabs'
			));

			register_admin_menu(array(
				'id' => 'custom_css',
				'title' => 'Custom CSS',
				'icon' => 'fas fa-sticky-note',
				'page' => 'custom-css',
				'access' => 'custom_css'
			));


			add_action('headers', 'headers');
			add_action('before_headers', 'before_headers');
			add_action("after_headers", "render_custom_css");
			add_action('footers', 'footers');
			add_action('get_image_uploader', 'display_image_uploader');
			add_action('drag_n_drop_uploader', 'drag_n_drop_uploader');
			add_action('add_db_section_to_form', 'add_db_section_to_form');
			add_action('delete_post_button', 'delete_post_button');
			add_action('profile_state', 'profile_state', 1);
			add_action('profile_state', 'login_as_user_btn', 2);
			add_action('_display_message', '_display_message');
			add_action('click_to_copy_button', 'click_to_copy_button');
			add_action("before_update_current_theme", "theme_install");
			add_action("more_settings_field_right", "public_registration_settings", 1);
			add_action('save_more_settings_field', 'save_public_registration_settings');
			add_action("registration", "default_registration_form", 1);
			add_action("registration", "default_registration_password_field", 92);
			add_action('load_more_api', 'load_more_api');
			add_action("dashboard_row", "unverified_dashboard", 1);
			add_action("comments_api", "comments_api");
			add_action("reviews_api", "reviews_api");
			add_action("add_user", "default_add_user");
			add_action('display_search_box', 'display_search_box');
			add_action('display_qf_search_box', 'display_qf_search_box');
			add_action('search_query_filter_date_range_only', 'search_query_date_range');
			add_action('search_query_filter_users', 'users_filter');
			add_action('search_query_filter_users', 'search_query_date_range');
			add_action('search_query_qf_filter_visitors', 'search_query_date_range_2');
			add_action('search_query_qf_filter_visitors', 'visitors_filter');
			add_action('search_query_qf_filter_external_visits', 'search_query_date_range_2');
			add_action('search_query_qf_filter_external_visits', 'visitors_filter');
			add_action('search_query_qf_filter_users', 'users_filter');
			add_action("ViewerJS_from_library", "viewerjs_from_library");
			add_action("build_filter_columns", "build_filter_columns");
			add_action("preset_controlled_loader", "preset_controlled_loader");
			add_action("croppie_api", "croppie_api");
			add_action("search_form", "search_form");
			add_action("search_users", "search_users");
			add_action("country_directories", "country_directories");
			add_action("menu_search", "menu_search");

			add_filter("capabilities", "filter_preset_capabilities");
			add_filter("review_rating_form", "review_rating_form");
			if(class_exists("cronjobs")) {
				$cron = new cronjobs();
				$cron->init();
			}
			if(class_exists("support") && class_exists("template")) {
				$support = new support;
				$support->register_templates();
			}
		// }
		if(!get_option("alter_cache_table")) {
			$sql = "ALTER TABLE `cache` CHANGE `selector` `selector` TEXT NOT NULL";
			$query = new query;
			$query->query($sql);
			update_option("alter_cache_table", 1);
		}
		if(!get_option("alter_visitors_table")) {
			$sql = "ALTER TABLE `visitors` CHANGE `page_link` `page_link` TEXT NOT NULL";
			$query = new query;
			$query->query($sql);
			update_option("alter_visitors_table", 1);
		}
		update_option('software_version', $this->version);
		$this->plugins->install();
		$this->theme->install(false);
		sleep(3);
		apply_common_access(['unverified']);
		apply_common_access(array(), $common_access);
		build_number_validation_regex();
	}

}