<!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 

class Beruco_Custom_Sidebars {
	
	private static $_instance = null;

	public function __construct() {
		add_action( 'admin_menu', array( $this, 'beruco_addon_admin_menu' ) );	
		add_action( 'wp_ajax_beruco-custom-sidebar-export', array( $this, 'beruco_custom_sidebar_export' ) );
	}
	
	public static function beruco_addon_admin_menu(){
		add_submenu_page( 
			'beruco-welcome', 
			esc_html__( 'Custom Sidebars', 'beruco-addon' ),
			esc_html__( 'Custom Sidebars', 'beruco-addon' ), 
			'manage_options', 
			'beruco-sidebars', 
			array( 'Beruco_Custom_Sidebars', 'beruco_sidebar_admin_page' )
		);
	}
	
	public static function beruco_sidebar_admin_page(){
		$beruco_theme = wp_get_theme();
	?>
		<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 Custom Sidebars', '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">
					<a href="<?php echo esc_url( 'https://wordpress.zozothemes.com/beruco/' ); ?>" class="button beruco-btn"><?php esc_html_e( 'Live Demo', 'beruco-addon' ); ?></a>
				</div><!-- .beruco-header-right -->
			</div><!-- .beruco-header-bar -->
			
			<div class="beruco-settings-tabs beruco-custom-sidebar-wrap">
				<div id="beruco-general" class="beruco-settings-tab active">
					<div class="container">
						<div class="row">
							<div class="col-4">
								<div class="media admin-box">
									<div class="admin-box-icon mr-3">
										<span class="dashicons dashicons-welcome-widgets-menus"></span>								
									</div>
									<div class="media-body admin-box-info">
										<h3 class="admin-box-title"><?php esc_html_e( 'Add New Sidebar', 'beruco-addon' ); ?></h3>
										<div class="admin-box-content">
											<?php esc_html_e( 'You can add new custom sidebar here. Also we give you option to remove or delete custom sidebars.', 'beruco-addon' ); ?>
										</div>
										<?php
											$sidebars = '';
											$sidebar_opt_name = 'beruco_custom_sidebars';
											$sidebars = get_option( $sidebar_opt_name );
											
											if ( isset( $_POST['beruco_custom_sidebar_nonce'] ) && wp_verify_nonce( $_POST['beruco_custom_sidebar_nonce'], 'beruco-()@)(*^#@!' ) 
											) {
												if (isset($_POST['beruco_sidebar_name']) && !empty($_POST['beruco_sidebar_name'])) {
													$sidebar_name = sanitize_text_field($_POST['beruco_sidebar_name']); // Sanitize
													$sidebar_slug = sanitize_title($sidebar_name);

													if (!empty($sidebars)) {
														$sidebars[$sidebar_slug] = $sidebar_name;
													}else{
														$sidebars = array( $sidebar_slug => $sidebar_name );
													}	
													update_option( 'beruco_custom_sidebars', $sidebars );
												}
											}
											
											if ( isset( $_POST['beruco_custom_sidebar_remove_nonce'] ) && wp_verify_nonce( $_POST['beruco_custom_sidebar_remove_nonce'], 'beruco-()I*^*^%@!' ) 
											) {
												$remove_sidebar = isset($_POST['beruco_sidebar_remove_name']) && !empty($_POST['beruco_sidebar_remove_name']) ? sanitize_text_field($_POST['beruco_sidebar_remove_name']) : ''; // Sanitize
												unset($sidebars[$remove_sidebar]);
												update_option('beruco_custom_sidebars', $sidebars);
												$sidebars = get_option($sidebar_opt_name);
											}
											
										?>
										<form action="" method="post" enctype="multipart/form-data">
											<?php wp_nonce_field( 'beruco-()@)(*^#@!', 'beruco_custom_sidebar_nonce' ); ?>
											<input type="input" name="beruco_sidebar_name" class="custom-sidebar-name" value="" />
										</form>
										<a href="#" class="beruco-btn btn-default custom-sidebar-create"><?php esc_html_e( 'Add', 'beruco-addon' ); ?></a>
 											<a href="widgets.php"> View Added Sidebars.</a>
									</div>
								</div>
							</div>
							<div class="col-8">
								<div class="admin-box">
									<h3 class="admin-box-title sidebar-title"><?php esc_html_e( 'Custom Sidebars', 'beruco-addon' ); ?></h3>
									<?php if( !empty( $sidebars ) ): ?>
									<form action="" method="post" enctype="multipart/form-data">
									<?php wp_nonce_field( 'beruco-()I*^*^%@!', 'beruco_custom_sidebar_remove_nonce' ); ?>
									<input type="hidden" name="beruco_sidebar_remove_name" id="beruco-sidebar-remove-name" value="" />									
									<table class="beruco-admin-table beruco-custom-sidebar-table">
										<thead>
											<tr>
												<td><?php esc_html_e( 'Name', 'beruco-addon' ); ?></td>
												<td><?php esc_html_e( 'Slug', 'beruco-addon' ); ?></td>
												<td><?php esc_html_e( 'Delete', 'beruco-addon' ); ?></td>
											</tr>
										</thead>
										<tbody>
										<?php
											foreach( $sidebars as $sidebar_slug => $sidebar_name ){
											?>
												<tr>
													<td><?php echo esc_html( $sidebar_name ); ?></td>
													<td><?php echo esc_html( $sidebar_slug ); ?></td>
													<td class="text-center"><a href="#" data-sidebar="<?php echo esc_attr( $sidebar_slug ); ?>" class="beruco-sidebar-remove"><span class="dashicons dashicons-trash"></span></a></td>
												</tr>
											<?php
											}
										?>
										</tbody>
									</table>
									</form>
									<a href="#" class="beruco-btn btn-default custom-sidebar-export"><?php esc_html_e( 'Export as JSON', 'beruco-addon' ); ?></a>
									<?php else: ?>
										<p><?php esc_html_e( 'Sorry! No custom sidebars available.', 'beruco-addon' ); ?></p>
									<?php endif; ?>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
			
		</div>
	<?php
	}
		
	public static function rmdir_recurse($path) {
		$path = rtrim($path, '/').'/';
		$handle = opendir($path);
		while(false !== ($file = readdir($handle))) {
			if($file != '.' and $file != '..' ) {
				$fullpath = $path.$file;
				if(is_dir($fullpath)) self::rmdir_recurse($fullpath); else unlink($fullpath);
			}
		}
		closedir($handle);
		rmdir($path);
	}	
	
	public static function beruco_custom_sidebar_export(){
		$nonce = $_POST['nonce'];  
		if ( ! wp_verify_nonce( $nonce, 'beruco-()@)(*^#@!' ) )
			wp_die ( esc_html__( 'Not Ok', 'beruco-addon' ) );
		
		$sidebars = get_option( 'beruco_custom_sidebars' );
		if( !empty( $sidebars ) ){
			//wp_send_json( $sidebars );
			echo json_encode( $sidebars );
		}else{
			echo '';
		}	
		wp_die();
	}
	
	public static function get_instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

} Beruco_Custom_Sidebars::get_instance();