<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
<?php

/**
 * Beruco Theme Options
 * @since 1.0.0
 */
final class Beruco_Plugin_Options { //beruco_admin_menu_out
	
	private static $_instance = null;
	
	public function __construct() {	
		add_action( 'admin_menu', array( $this, 'beruco_addon_options_menu' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'beruco_framework_admin_scripts' ) );
		$this->init();

		//import
		add_action( 'wp_ajax_bridddge-theme-option-import', array( $this, 'beruco_redux_themeopt_import' ) );

		//export
		add_action('wp_ajax_beruco-theme-options-export', array( $this, 'beruco_theme_options_export' ) );
		
	}
	
	public static function beruco_addon_options_menu(){
		add_submenu_page( 
			'beruco-welcome', 
			esc_html__( 'Theme Options', 'beruco-addon' ),
			esc_html__( 'Theme Options', 'beruco-addon' ), 
			'manage_options', 
			'beruco-options', 
			array( 'Beruco_Plugin_Options', 'beruco_options_admin_page' )
		);
	}
	
	public static function beruco_framework_admin_scripts(){
		if( isset( $_GET['page'] ) && $_GET['page'] == 'beruco-options' ){
			wp_enqueue_style( 'font-awesome', BERUCO_ADDON_URL . 'admin/extension/theme-options/assets/css/font-awesome.min.css', array(), '4.7.0', 'all' );			
			wp_enqueue_style( 'bootstrap-icons', BERUCO_URI . '/assets/css/bootstrap-icons.css', array(), '1.9.1', 'all' );
			
			wp_enqueue_media();
			wp_enqueue_style( 'beruco_theme_options_css', BERUCO_ADDON_URL . 'admin/extension/theme-options/assets/css/theme-options.css', array(), '1.0', 'all' );
			wp_enqueue_style( 'wp-color-picker');
			wp_enqueue_script( 'wp-color-picker-alpha', BERUCO_ADDON_URL . 'admin/extension/theme-options/assets/js/wp-color-picker-alpha.min.js', array( 'jquery', 'wp-color-picker' ), '3.0.0' );
			wp_enqueue_script( 'beruco_theme_options_js', BERUCO_ADDON_URL . 'admin/extension/theme-options/assets/js/theme-options.js', array( 'jquery' ), '1.0', true );

			wp_localize_script( 'beruco_theme_options_js', 'beruco_ajax_object',
				array(
					'import_nonce' => wp_create_nonce( 'beruco-import-*&^F&' ),
					'export_nonce' => wp_create_nonce( 'beruco-export-&^%$)' ),
				)
			);

			require_once BERUCO_ADDON_DIR . 'admin/extension/theme-options/googlefonts.php';
			$google_fonts = Beruco_Google_Fonts_Function::$_google_fonts;
			$google_fonts_arr = json_decode( $google_fonts, true );
			
			$extra_gf = array(
				"Spartan" => array(
					"variants" => array(
						array( "id" => "400", "name" => "Thin 100" ),
						array( "id" => "400", "name" => "Extra-light 200" ),
						array( "id" => "400", "name" => "Light 300" ),
						array( "id" => "400", "name" => "Regular 400" ),
						array( "id" => "400", "name" => "Medium 500" ),
						array( "id" => "400", "name" => "Semi-bold 600" ),
						array( "id" => "400", "name" => "Bold 700" ),
						array( "id" => "400", "name" => "Extra-bold 800" ),
						array( "id" => "400", "name" => "Black 900" )
					)
				)
			);
			if( is_array( $extra_gf ) && !empty( $extra_gf ) ){
				foreach( $extra_gf as $font => $details ) $google_fonts_arr[$font] = $details;
			}
			
			$google_fonts = json_encode( $google_fonts_arr );
			$google_fonts_vars = array(
				'google_fonts' => $google_fonts,
				'standard_font_variants' => Beruco_Google_Fonts_Function::$_standard_font_variants,
				'font_variants_default' => esc_html__( 'Font Weight &amp; Style', 'beruco-addon' ),
				'font_sub_default' => esc_html__( 'Font Subsets', 'beruco-addon' )
			);
			wp_localize_script( 'beruco_theme_options_js', 'google_fonts_vars', $google_fonts_vars );
			
		}
	}
	
	public function init() {
		require_once( BERUCO_ADDON_DIR . 'admin/extension/theme-options/framework.php' );
		Beruco_Options::$opt_name = 'beruco_options';
	}
		
	public static function beruco_options_admin_page(){	
		$beruco_theme = wp_get_theme(); ?>	
		<form method="post" action="#" enctype="multipart/form-data" id="beruco-plugin-form-wrapper">
			<div class="beruco-settings-wrap">
			
				<div class="beruco-header-bar">
					<div class="beruco-header-left">
						<div class="beruco-admin-logo-inline">
							<img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/brand-logo.png' ); ?>" alt="beruco-logo">
						</div><!-- .beruco-admin-logo-inline -->
						<h2 class="title"><?php esc_html_e( 'Beruco Options', 'beruco-addon' ); ?><span class="beruco-version"><?php echo esc_attr( $beruco_theme->get( 'Version' ) ); ?></span></h2>
					</div><!-- .beruco-header-left -->
					<div class="beruco-header-right">
						<button type="submit" class="button beruco-btn"><?php esc_html_e( 'Save Settings', 'beruco-addon' ); ?></button>
					</div><!-- .beruco-header-right -->
				</div><!-- .beruco-header-bar -->				
				
				<div class="beruco-inner-wrap">
						
					<?php
						
						if ( isset( $_POST['save_beruco_theme_options'] ) && wp_verify_nonce( $_POST['save_beruco_theme_options'], 'beruco_theme_options*&^&*$' ) ) {
							update_option( 'beruco_options', $_POST['beruco_options'] );
							require_once ( BERUCO_ADDON_DIR . 'admin/extension/theme-options/theme-options-css.php' );
						}
						
						//Get updated theme option
						Beruco_Options::$beruco_options = get_option('beruco_options');
						
						if( class_exists( 'Classic_Elementor_Addon' ) ){
							add_action( 'beruco_custom_template_options', function(){
								require_once BERUCO_ADDON_DIR . 'admin/extension/theme-options/cea-config.php';
							});
						}
						
						//Theme config
						require_once BERUCO_ADDON_DIR . 'admin/extension/theme-options/config.php';
						
					?>
					
					<div class="beruco-admin-content-wrap">
						<?php wp_nonce_field( 'beruco_theme_options*&^&*$', 'save_beruco_theme_options' ); ?>
						<div class="beruco-tab">
							<div class="beruco-tab-list">
								<ul class="tablinks-list">
									<?php Beruco_Options::beruco_put_section(); 
										if (is_plugin_active('classic-elementor-addons-pro/index.php')): ?>
											<div class="beruco-navigation-buttons">
												<a href="<?php echo admin_url('admin.php?page=classic-addons'); ?>" class="button beruco-btn"><i class="fa fa-caret-square-o-left" aria-hidden="true"></i><?php esc_html_e('CEA Plugin Options', 'beruco'); ?></a>
											</div>
										<?php endif; ?>	
								</ul>
							</div><!-- .beruco-tab-list -->
							<div class="beruco-tab-contents">
								<?php Beruco_Options::beruco_put_field(); ?>
							</div><!-- .beruco-tab-contents -->
						</div><!-- .beruco-tab -->							
					</div><!-- .beruco-admin-content-wrap -->					
				</div><!-- .beruco-inner-wrap -->
			</div><!-- .beruco-settings-wrap -->
		</form>	
	<?php
	}

	public static function beruco_theme_options_export(){
		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';
		if ( ! wp_verify_nonce( $nonce, 'beruco-export-&^%$)' ) )
			die ( esc_html__( 'Busted!', 'beruco-addon' ) );
		
		$beruco_options = get_option( 'beruco_options');
		$beruco_options = is_array( $beruco_options ) ? array_map( 'stripslashes_deep', $beruco_options ) : stripslashes( $beruco_options );
		echo json_encode( $beruco_options );
		
		exit;
	}

	public static function beruco_redux_themeopt_import(){
		$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : ''; //Sanitize
		if ( ! wp_verify_nonce( $nonce, 'beruco-import-*&^F&' ) )
			die ( esc_html__( 'Busted', 'beruco-addon' ) );
		
		$json_data = isset( $_POST['json_data'] ) ? stripslashes( urldecode( $_POST['json_data'] ) ) : '';
		$theme_opt_arr = json_decode( $json_data, true );
		if( !empty( $theme_opt_arr ) ){
			update_option( 'beruco_options', $theme_opt_arr );
		}
		
		wp_die('success');
	}
	
	public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

}

Beruco_Plugin_Options::instance();