<?php
/*
*	Admin Custom Sidebars
*
* 	@author		Euthemians Team
* 	@URI		http://euthemians.com
*/

	function engic_eutf_add_sidebar_settings() {

		if ( isset( $_POST['_engic_eutf_nonce_sidebars_save'] ) && wp_verify_nonce( $_POST['_engic_eutf_nonce_sidebars_save'], 'engic_eutf_nonce_sidebars_save' ) ) {

			$sidebars_items = array();
			if( isset( $_POST['_engic_eutf_custom_sidebar_item_id'] ) ) {
				$num_of_sidebars = sizeof( $_POST['_engic_eutf_custom_sidebar_item_id'] );
				for ( $i=0; $i < $num_of_sidebars; $i++ ) {
					$this_sidebar = array (
						'id' => $_POST['_engic_eutf_custom_sidebar_item_id'][ $i ],
						'name' => $_POST['_engic_eutf_custom_sidebar_item_name'][ $i ],
					);
					array_push( $sidebars_items, $this_sidebar );
				}
			}
			if ( empty( $sidebars_items ) ) {
				delete_option( '_engic_eutf_custom_sidebars' );
			} else {
				update_option( '_engic_eutf_custom_sidebars', $sidebars_items );
			}
			//Update Sidebar list
			wp_get_sidebars_widgets();
			wp_safe_redirect( 'themes.php?page=engic-eutf-custom-sidebar-settings&sidebar-settings=saved' );

		}

		add_theme_page(
			esc_html__( 'Sidebars', 'engic' ),
			esc_html__( 'Sidebars', 'engic' ),
			'manage_options',
			'engic-eutf-custom-sidebar-settings',
			'engic_eutf_show_sidebar_settings'
		);
	}

	add_action( 'admin_menu', 'engic_eutf_add_sidebar_settings' );

	function engic_eutf_show_sidebar_settings() {
		$engic_eutf_custom_sidebars = get_option( '_engic_eutf_custom_sidebars' );
?>
	<div id="eut-sidebar-wrap" class="wrap">
		<h2><?php echo esc_html__( "Sidebars", 'engic' ); ?></h2>

		<?php if( isset( $_GET['sidebar-settings'] ) ) { ?>
		<div class="eut-sidebar-saved eut-notice-green">
			<strong><?php esc_html_e( 'Settings Saved!', 'engic' ); ?></strong>
		</div>
		<?php } ?>
		<input type="text" id="eut-custom-sidebar-item-name-new" value=""/>
		<input type="button" id="eut-add-custom-sidebar-item" class="button button-primary" value="<?php esc_attr_e( 'Add New', 'engic' ); ?>"/>
		<span class="eut-sidebar-spinner"></span>
		<div class="eut-sidebar-notice eut-notice-red" style="display:none;">
			<strong><?php esc_html_e( 'Field must not be empty!', 'engic' ); ?></strong>
		</div>
		<div class="eut-sidebar-notice-exists eut-notice-red" style="display:none;">
			<strong><?php esc_html_e( 'Sidebar with this name already exists!', 'engic' ); ?></strong>
		</div>
		<form method="post" action="themes.php?page=engic-eutf-custom-sidebar-settings">
			<?php wp_nonce_field( 'engic_eutf_nonce_sidebars_save', '_engic_eutf_nonce_sidebars_save' ); ?>
			<div id="eut-custom-sidebar-container">
				<?php engic_eutf_print_admin_custom_sidebars( $engic_eutf_custom_sidebars ); ?>
			</div>
			<?php submit_button(); ?>
		</form>
	</div>
<?php
	}

	function engic_eutf_print_admin_custom_sidebars( $engic_eutf_custom_sidebars ) {


		if ( ! empty( $engic_eutf_custom_sidebars ) ) {
			foreach ( $engic_eutf_custom_sidebars as $engic_eutf_custom_sidebar ) {
				engic_eutf_print_admin_single_custom_sidebar( $engic_eutf_custom_sidebar );
			}
		}
	}

	function engic_eutf_print_admin_single_custom_sidebar( $sidebar_item, $mode = '' ) {

		$engic_eutf_button_class = "eut-custom-sidebar-item-delete-button";
		$sidebar_item_id = uniqid('engic_eutf_sidebar_');

		if( $mode = "new" ) {
			$engic_eutf_button_class = "eut-custom-sidebar-item-delete-button eut-item-new";
		}
?>


	<div class="eut-custom-sidebar-item">
		<input class="<?php echo esc_attr( $engic_eutf_button_class ); ?> button" type="button" value="<?php esc_attr_e( 'Delete', 'engic' ); ?>">
		<h3 class="eut-custom-sidebar-title">
			<span><?php esc_html_e( 'Custom Sidebar', 'engic' ); ?>: <?php echo engic_eutf_array_value( $sidebar_item, 'name' ); ?></span>
		</h3>
		<div class="eut-custom-sidebar-settings">
			<input type="hidden" name="_engic_eutf_custom_sidebar_item_id[]" value="<?php echo engic_eutf_array_value( $sidebar_item, 'id', $sidebar_item_id ); ?>">
			<input type="hidden" class="eut-custom-sidebar-item-name" name="_engic_eutf_custom_sidebar_item_name[]" value="<?php echo engic_eutf_array_value( $sidebar_item, 'name' ); ?>"/>
		</div>
	</div>

<?php

	}

	add_action( 'wp_ajax_engic_eutf_get_custom_sidebar', 'engic_eutf_get_custom_sidebar' );

	function engic_eutf_get_custom_sidebar() {



		if( isset( $_POST['sidebar_name'] ) ) {

			$sidebar_item_name = $_POST['sidebar_name'];
			$sidebar_item_id = uniqid('engic_eutf_sidebar_');
			if( empty( $sidebar_item_name ) ) {
				$sidebar_item_name = $sidebar_item_id;
			}

			$this_sidebar = array (
				'id' => $sidebar_item_id,
				'name' => $sidebar_item_name,
			);

			engic_eutf_print_admin_single_custom_sidebar( $this_sidebar, 'new' );
		}
		die();

	}

//Omit closing PHP tag to avoid accidental whitespace output errors.
