<?php
/**
 * WP Submenu and Add and Update Options
 *
 * @since 1.0.0
 * @todo Replace only if your creating your own Plugin
 * @todo hpw - Find all and replace text
 * @todo Hide Prices Woocommerce - Find all and replace text
 */

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

/**
 * Sub menu hooks (unused)
 * 
 *
 * @since 1.0
 * @return void
 */

function hpw_register_menu() {
	add_options_page( 
		__( 'Set Hide Prices Woocommerce', 'hpw-txt' ), // Page Title
		__( 'Hide Prices Woocommerce', 'hpw-txt' ), // Menu Title
		 'manage_options', // Capability
		 'hpw-menu-page', // Menu Slug 
		 'hpw_admin_menu_page_callback' ); // Function
}
add_action( 'admin_menu', 'hpw_register_menu');

/**
 * WP Options (Get, Update, and Add)
 *
 * @see Options API (https://codex.wordpress.org/Options_API)
 */
function hpw_admin_menu_page_callback() {

	// $_POST needs to be sanitized
	if(isset($_POST['submit'])
		&& check_admin_referer('hpw_option_action','hpw_option_field') // @see WP Docs for check_admin_referer()
	){

		/** Array DB $options; if emtpy assign empty string */
		$options = array(
			'hpw_option_1' => isset($_POST["hpw_option_1"]) ? $_POST["hpw_option_1"]  : "",
			'hpw_option_2' => isset($_POST["hpw_option_2"]) ? $_POST["hpw_option_2"]  : "",
			'hpw_option_3' => isset($_POST["hpw_option_3"]) ? $_POST["hpw_option_3"]  : "",
		);

		/* Handling var Array */	
		foreach($options as $option_name => $option_value) {
			// If option name exist, update it; else add it!
			if ( get_option( $option_name ) !== false ) {
				update_option($option_name, $option_value);
			} else {	
				add_option( $option_name, $option_value, '', 'yes');
			}
		}
	}
?>	
	<div id="hpw-preloader-setting-page" class="wrap">
		<h1><?php _e('Hide Prices Woocommerce Settings', 'hpw-txt'); ?></h1>
		<span class="title"><?php _e('Configuration Settings for Hide Prices Woocommerce Plugin', 'hpw-txt'); ?></span>

		<form method="post" action="<?php echo esc_attr($_SERVER["REQUEST_URI"]); ?>">
			<?php wp_nonce_field('hpw_option_action','hpw_option_field'); ?>
			<table class="form-table form-table-1">
				<tbody>
					<tr>
						<td>
						<h3><label for="general">General Settings</label></h3>
						<p>
							<input class="hpw_option_1" name="hpw_option_1" type="text" value="<?php echo get_option('hpw_option_1', 'Login to see prices'); ?>">
							<label><?php _e('<i>Default: Login to see prices.</i> <b>Must Log-in Label</b>', 'hpw-txt'); ?></label>
						</p>
						<p>
							<input class="hpw_option_2" name="hpw_option_2" type="url" placeholder="http://" value="<?php echo get_option('hpw_option_2', get_permalink(woocommerce_get_page_id('myaccount'))); ?>">
							<label><?php _e('<b>Link to Login Page</b>.', 'hpw-txt'); ?></label>
						</p>
						<p>
							<input class="hpw_option_3" name="hpw_option_3" type="text" value="<?php echo get_option('hpw_option_3', "Waiting for verification"); ?>">
							<label><?php _e('<i>Default: Waiting for verification.</i> <b>Must verify by the admin</b>', 'hpw-txt'); ?></label>
						</p>
						<p>Note: Make sure you have woocommerce plugin installed.</p>
				
						</td>
					</tr>
	
				</tbody>
			</table>
			<p class="submit"><input type="submit" name="submit" class="button-primary" value="<?php _e('Save Changes', 'hpw-txt'); ?>" /></p>
		</form>
	</div>

<?php 	
}





