<?php

register_activation_hook( __FILE__, 'eq3t_set_default_options' );


function eq3t_set_default_options() { 
	eq3t_get_options();
}

/**
 * Get client options
 * @return type
 */
function eq3t_get_options() {
    $options = get_option( 'eq3t_options', array() );

    $new_options['client_key'] = ''; 
    $new_options['client_id'] = '';
    $new_options['remote_url'] = '';
    $new_options['parent_type'] = 0;
    $new_options['parent_id'] = 0;
    $new_options['link_addr'] = '';
	
    $merged_options = wp_parse_args( $options, $new_options ); 

    $compare_options = array_diff_key( $new_options, $options );   
    if ( empty( $options ) || !empty( $compare_options ) ) {
        update_option( 'eq3t_options', $merged_options );
    }
    return $merged_options;
}

// Register action hook function to be called when the admin pages are
// starting to be prepared for display
add_action( 'admin_init', 'eq3t_admin_init' );

// Function to register the Settings for this plugin
// and declare the fields to be displayed
function eq3t_admin_init() {
	// Register our setting group with a validation function
	// so that $_POST handling is done automatically for us
	register_setting( 'eq3t_settings',
		'eq3t_options', 'eq3t_validate_options' );

	// Add a new settings section within the group
	add_settings_section( 'eq3t_main_section',
		'Main Settings', 'eq3t_main_setting_section_callback',
		'eq3t_settings_section' );
  
	// Add the fields with the names and function to use for our new
	// settings, put them in our new section
	add_settings_field('client_key', 'Client Key', 'eq3t_display_text_field', 
      'eq3t_settings_section', 'eq3t_main_section', 
      array('name' => 'client_key',
        'size' => 50));

  add_settings_field('client_id', 'Client ID', 'eq3t_display_text_field', 
      'eq3t_settings_section', 'eq3t_main_section', array('name' => 'client_id'));

  add_settings_field('remote_url', 'Remote URL', 'eq3t_display_text_field',
      'eq3t_settings_section', 'eq3t_main_section', array('name' => 'remote_url', 'size' => 50));
  
  	add_settings_field('link_addr', 'Link Address', 'eq3t_display_text_field', 
      'eq3t_settings_section', 'eq3t_main_section', 
      array('name' => 'link_addr',
        'size' => 100));

}

// Validation function to be called when data is posted by user
// No validation done at this time. Straight return of values.
function eq3t_validate_options( $input ) {
    // Cycle through all text form fields and store their values 
    // in the options array 
    foreach ( array( 'ga_account_name' ) as $option_name ) { 
        if ( isset( $input[$option_name] ) ) { 
            $input[$option_name] = 
                sanitize_text_field( $input[$option_name] ); 
        } 
    } 

    // Cycle through all check box form fields and set the options 
    // array to true or false values based on presence of 
    // variables 
    foreach ( array( 'track_outgoing_links' ) as $option_name ) { 
        if ( isset( $input[$option_name] ) ) { 
            $input[$option_name] = true; 
        } else { 
            $input[$option_name] = false; 
        } 
    }
	
	return $input;
}


// Function to display text at the beginning of the main section
function eq3t_main_setting_section_callback() { 
	print '<p>Client Settings section.</p>';
}

// Function to render a text input field
function eq3t_display_text_field( $data = array() ) {
	extract( $data );
  if(!isset($size)) {
    $size = 20;
  }
	$options = eq3t_get_options(); 
	?>
<input type="text" size="<?php echo $size; ?>" name="eq3t_options[<?php echo $name; ?>]" value="<?php echo esc_html( $options[$name] ); ?>"/><br />

<?php }

// Function to render a check box
function eq3t_display_check_box( $data = array() ) {
	extract ( $data );
	$options = eq3t_get_options(); 
	?>
	<input type="checkbox" name="eq3t_options[<?php echo $name; ?>]" <?php if ( $options[$name] ) echo ' checked="checked" '; ?>/>
<?php }

// Register action hook to be called when the administration menu is
// being constructed
add_action( 'admin_menu', 'eq3t_settings_menu' );

// Function called when the admin menu is constructed to add a new menu item
// to the structure
function eq3t_settings_menu() {
	add_options_page( 'Enquir3 Testimonial',
		'Enquir3 Testimonial', 'manage_options',
		'eq3t_settings', 'eq3t_config_page' );
}

// Function called to render the contents of the plugin
// configuration page
function eq3t_config_page() { ?>
	<div id="eq3t-general" class="wrap">
	<h2>Enquir3 Testimonial Settings</h2>

	<form name="eq3t_options_form_settings_api" method="post" action="options.php">

	<?php settings_fields( 'eq3t_settings' ); ?>
	<?php do_settings_sections( 'eq3t_settings_section' ); ?> 

	<input type="submit" value="Submit" class="button-primary" />
	</form>
	</div>
<?php }
