/**
 * function wpminv_register_settings()
 */
function wpminv_register_settings()
{
    /* build array of setttings to register */
    $registered_settings = wpminv_get_admin_settings();
    /* check we have some settings to register */
    if (empty($registered_settings)) {
        return;
    }
    /* loop through registered settings array */
    foreach ($registered_settings as $registered_setting) {
        /* if the setting has a sanitize callback */
        if (!empty($registered_setting['sanitize_callback'])) {
            $sanitize_callback = $registered_setting['sanitize_callback'];
        } else {
            $sanitize_callback = null;
        }
        /* build the setting name */
        $registered_setting_name = apply_filters('wpminv_admin_setting_prefix', 'wpminv_') . $registered_setting['name'];
        /* register a new setting with wp in the wpminv_settings group */
        register_setting('wpminv_admin_settings', $registered_setting_name, $sanitize_callback);
    }
}
/**
 * function wpminv_settings_content()
 *
 * output content for the settings menu page
 */
function wpminv_settings_content()
{
    ?>
	
	<div class="wrap">
		
		<h1><?php 
    _e('Invoicing Settings', 'simvoicing');
    ?>
</h1>
		
		<?php 
    /**
     * @hook - wpminv_before_admin_settings_form
     */
    do_action('wpminv_before_admin_settings_form');
    ?>
		
		<form method="post" action="<?php 
    echo esc_url(admin_url('options.php'));
    ?>
">
			
			<?php 
    /* output settings field nonce action fields etc. */
    settings_fields('wpminv_admin_settings');
    /**
     * @hook - wpbb_before_form_settings
     */
    do_action('wpbb_before_form_settings');
    /* get all the registered settings */
    $wpminv_settings = wpminv_get_admin_settings();
    ?>
					
					<table class="form-table">
							
						<tbody>
							
							<?php 
    /* loop through each setting */
    foreach ($wpminv_settings as $setting) {
        /* add plugin prefix to setting name */
        $setting_name = apply_filters('wpminv_admin_setting_prefix', 'wpminv_') . $setting['name'];
        ?>
									<tr valign="top">
										
										<th scope="row">
											<label for="<?php 
        echo esc_attr($setting_name);
        ?>
"><?php 
        echo esc_html($setting['label']);
        ?>
</label>
										</th>
										
										<td>
										
										<?php 
        /* first lets switch depending on the setting type */
        switch ($setting['type']) {
            /* if the setting is a select input */
            case 'select':
                ?>
											    	<select name="<?php 
                echo esc_attr($setting_name);
                ?>
" id="<?php 
                echo esc_attr($setting_name);
                ?>
">
											    	
											    	<?php 
                /* loop through each option */
                foreach ($setting['options'] as $setting_option) {
                    ?>
												        <option value="<?php 
                    echo esc_attr($setting_option['value']);
                    ?>
" <?php 
                    selected(get_option($setting_name), esc_attr($setting_option['value']));
                    ?>
><?php 
                    echo esc_html($setting_name);
                    ?>
</option>
														<?php 
                }
                ?>
											    	</select>
											        <?php 
                break;
                /* if the setting is a wysiwyg input */
            /* if the setting is a wysiwyg input */
            case 'wysiwyg':
                /* get the number of textarea rows to add */
                if (!empty($setting['textarea_rows'])) {
                    $textarea_rows = $setting['textarea_rows'];
                } else {
                    $textarea_rows = '5';
                }
                /* check if we have textarea rows */
                if (!isset($setting['media_buttons']) || empty($setting['media_buttons'])) {
                    $media_buttons = false;
                } else {
                    $media_buttons = $setting['media_buttons'];
                }
                /* set some settings args for the editor */
                $editor_settings = array('textarea_rows' => $textarea_rows, 'media_buttons' => $media_buttons, 'textarea_name' => esc_attr($setting_name));
                /* display the wysiwyg editor */
                wp_editor(wpautop(get_option($setting_name)), $setting_name, $editor_settings);
                break;
                /* if the setting is a textarea input */
            /* if the setting is a textarea input */
            case 'textarea':
                /* get the number of textarea rows to add */
                if (!empty($setting['textarea_rows'])) {
                    $textarea_rows = $setting['textarea_rows'];
                } else {
                    $textarea_rows = '5';
                }
                ?>
													<textarea rows="<?php 
                echo esc_attr(absint($textarea_rows));
                ?>
" cols="40" name="<?php 
                echo esc_attr($setting_name);
                ?>
" id="<?php 
                echo esc_attr($setting_name);
                ?>
" class="large-text"><?php 
                echo sanitize_text_field(get_option($setting_name));
                ?>
</textarea>
													<?php 
                break;
                /* if the setting is a text input */
            /* if the setting is a text input */
            case 'text':
                ?>
													<input type="text" name="<?php 
                echo esc_attr($setting_name);
                ?>
" id="<?php 
                echo esc_attr($setting_name);
                ?>
" class="large-text" value="<?php 
                echo sanitize_text_field(get_option($setting_name));
                ?>
" />
													<?php 
                break;
                /* if the setting is a number input */
            /* if the setting is a number input */
            case 'number':
                ?>
													<input type="number" name="<?php 
                echo esc_attr($setting_name);
                ?>
" id="<?php 
                echo esc_attr($setting_name);
                ?>
" class="large-text" value="<?php 
                echo sanitize_text_field(get_option($setting_name));
                ?>
" class="large-text" />
													<?php 
                break;
                /* if the setting is a email input */
            /* if the setting is a email input */
            case 'email':
                ?>
													<input type="email" name="<?php 
                echo esc_attr($setting_name);
                ?>
" id="<?php 
                echo esc_attr($setting_name);
                ?>
" class="large-text" value="<?php 
                echo sanitize_text_field(get_option($setting_name));
                ?>
" class="large-text" />
													<?php 
                break;
                /* if setting provided is anything else */
            /* if setting provided is anything else */
            default:
                /**
                 *
                 */
                do_action('wpminv_setting_type_' . $setting['type'], $setting, $setting_name);
                break;
        }
        // end switch
        ?>
									
										</td>
									
									</tr>
								
								<?php 
    }
    // end loop through each setting
    ?>
							
						</tbody>
						
					</table>
					
					<?php 
    ?>
			
			<p class="submit">
				<input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes">
			</p>
			
		</form><!-- // end form -->

		<?php 
    /**
     * @hook - wpminv_after_admin_settings_form
     */
    do_action('wpminv_after_admin_settings_form');
    ?>
		
	</div>
	
	<?php 
}