<?php
if ( ! defined( 'ABSPATH' ) ) exit;

add_action( 'admin_menu', 'cybersource_fields_options_page' );

function cybersource_fields_options_page() {

    add_menu_page(
        'Cybersource Fields', // page title
        'Cybersource Fields', // menu title
        'manage_options', // capability to access the page
        'cybersource-fields', // menu slug
        'cybersource_fields_page_content', // callback function
        'dashicons-layout', // menu icon
        5 // position
    );

}

function cybersource_fields_page_content() {
	?>
    <div class="wrap">
		<h1><?php echo esc_html__("Cybersource Field Settings");?></h1>
		<form method="post" action="options.php">
		<?php		
			settings_fields( 'cybersource_fields_settings' ); // settings group name
			do_settings_sections( 'cybersource-fields' ); // a page slug
			submit_button();
		?>
		</form>
	</div>
	<?php
}


add_action( 'admin_init',  'cybersource_fields_register_setting' );

function cybersource_fields_register_setting() {

	add_settings_section(
		'homepage_section', // section ID
		'', // title
		'', // callback function
		'cybersource-fields' // page slug
	);
	
	// first field
	add_settings_field(
		'ship_to_address_line1_text',
		'ship_to_address_line1', //Label Name
		'ship_to_address_line1_text_field_html', // function which prints the field
		'cybersource-fields', // page slug
		'homepage_section', // section ID
		array( 
			'label_for' => 'ship_to_address_line1_text',
			'class' => 'banner-text',
		)
	);

	register_setting(
		'cybersource_fields_settings', // settings group name
		'ship_to_address_line1_text', // field name
		'sanitize_text_field' // sanitization function
	);

	// second field
  add_settings_field(
		'ship_to_address_line2_text',
		'ship_to_address_line2', //Label Name
		'ship_to_address_line2_text_field_html', // function which prints the field
		'cybersource-fields', // page slug
		'homepage_section', // section ID
		array( 
			'label_for' => 'ship_to_address_line1_text',
			'class' => 'banner-text',
		)
	);

	register_setting(
		'cybersource_fields_settings', // settings group name
		'ship_to_address_line2_text', // field name
		'sanitize_text_field' // sanitization function
	);


}

function ship_to_address_line1_text_field_html() {
	$ship_to_address_line1_text = get_option( 'ship_to_address_line1_text' );
	echo '<input type="text" id="ship_to_address_line1_text" name="ship_to_address_line1_text" value="'.$ship_to_address_line1_text.'" />';
}

function ship_to_address_line2_text_field_html() {
	$ship_to_address_line2_text = get_option( 'ship_to_address_line2_text' );
	echo '<input type="text" id="ship_to_address_line2_text" name="ship_to_address_line2_text" value="'.$ship_to_address_line2_text.'" />';
}

