/**
 * Add all settings sections and fields
 *
 * @since 1.0
 * @return void
*/
function edd_register_settings()
{
    if (false == get_option('edd_settings')) {
        add_option('edd_settings');
    }
    foreach (edd_get_registered_settings() as $tab => $sections) {
        foreach ($sections as $section => $settings) {
            // Check for backwards compatibility
            $section_tabs = edd_get_settings_tab_sections($tab);
            if (!is_array($section_tabs) || !array_key_exists($section, $section_tabs)) {
                $section = 'main';
                $settings = $sections;
            }
            add_settings_section('edd_settings_' . $tab . '_' . $section, __return_null(), '__return_false', 'edd_settings_' . $tab . '_' . $section);
            foreach ($settings as $option) {
                // For backwards compatibility
                if (empty($option['id'])) {
                    continue;
                }
                $name = isset($option['name']) ? $option['name'] : '';
                add_settings_field('edd_settings[' . $option['id'] . ']', $name, function_exists('edd_' . $option['type'] . '_callback') ? 'edd_' . $option['type'] . '_callback' : 'edd_missing_callback', 'edd_settings_' . $tab . '_' . $section, 'edd_settings_' . $tab . '_' . $section, array('section' => $section, 'id' => isset($option['id']) ? $option['id'] : null, 'desc' => !empty($option['desc']) ? $option['desc'] : '', 'name' => isset($option['name']) ? $option['name'] : null, 'size' => isset($option['size']) ? $option['size'] : null, 'options' => isset($option['options']) ? $option['options'] : '', 'std' => isset($option['std']) ? $option['std'] : '', 'min' => isset($option['min']) ? $option['min'] : null, 'max' => isset($option['max']) ? $option['max'] : null, 'step' => isset($option['step']) ? $option['step'] : null, 'chosen' => isset($option['chosen']) ? $option['chosen'] : null, 'placeholder' => isset($option['placeholder']) ? $option['placeholder'] : null, 'allow_blank' => isset($option['allow_blank']) ? $option['allow_blank'] : true, 'readonly' => isset($option['readonly']) ? $option['readonly'] : false, 'faux' => isset($option['faux']) ? $option['faux'] : false));
            }
        }
    }
    // Creates our settings in the options table
    register_setting('edd_settings', 'edd_settings', 'edd_settings_sanitize');
}
/**
 * Options Page
 *
 * Renders the options page contents.
 *
 * @since 1.0
 * @return void
 */
function edd_options_page()
{
    $settings_tabs = edd_get_settings_tabs();
    $settings_tabs = empty($settings_tabs) ? array() : $settings_tabs;
    $active_tab = isset($_GET['tab']) && array_key_exists($_GET['tab'], $settings_tabs) ? $_GET['tab'] : 'general';
    $sections = edd_get_settings_tab_sections($active_tab);
    $key = 'main';
    if (is_array($sections)) {
        $key = key($sections);
    }
    $registered_sections = edd_get_settings_tab_sections($active_tab);
    $section = isset($_GET['section']) && !empty($registered_sections) && array_key_exists($_GET['section'], $registered_sections) ? $_GET['section'] : $key;
    ob_start();
    ?>
	<div class="wrap">
		<h1 class="nav-tab-wrapper">
			<?php 
    foreach (edd_get_settings_tabs() as $tab_id => $tab_name) {
        $tab_url = add_query_arg(array('settings-updated' => false, 'tab' => $tab_id));
        // Remove the section from the tabs so we always end up at the main section
        $tab_url = remove_query_arg('section', $tab_url);
        $active = $active_tab == $tab_id ? ' nav-tab-active' : '';
        echo '<a href="' . esc_url($tab_url) . '" title="' . esc_attr($tab_name) . '" class="nav-tab' . $active . '">';
        echo esc_html($tab_name);
        echo '</a>';
    }
    ?>
		</h1>
		<?php 
    $number_of_sections = count($sections);
    $number = 0;
    if ($number_of_sections > 1) {
        echo '<div><ul class="subsubsub">';
        foreach ($sections as $section_id => $section_name) {
            echo '<li>';
            $number++;
            $tab_url = add_query_arg(array('settings-updated' => false, 'tab' => $active_tab, 'section' => $section_id));
            $class = '';
            if ($section == $section_id) {
                $class = 'current';
            }
            echo '<a class="' . $class . '" href="' . esc_url($tab_url) . '">' . $section_name . '</a>';
            if ($number != $number_of_sections) {
                echo ' | ';
            }
            echo '</li>';
        }
        echo '</ul></div>';
    }
    ?>
		<div id="tab_container">
			<form method="post" action="options.php">
				<table class="form-table">
				<?php 
    settings_fields('edd_settings');
    if ('main' === $section) {
        do_action('edd_settings_tab_top', $active_tab);
    }
    do_action('edd_settings_tab_top_' . $active_tab . '_' . $section);
    do_settings_sections('edd_settings_' . $active_tab . '_' . $section);
    do_action('edd_settings_tab_bottom_' . $active_tab . '_' . $section);
    // For backwards compatibility
    if ('main' === $section) {
        do_action('edd_settings_tab_bottom', $active_tab);
    }
    ?>
				</table>
				<?php 
    submit_button();
    ?>
			</form>
		</div><!-- #tab_container-->
	</div><!-- .wrap -->
	<?php 
    echo ob_get_clean();
}
/**
 * Options Page
 *
 * Renders the options page contents.
 *
 * @since 1.0
 * @return void
 */
function edd_options_page()
{
    $settings_tabs = edd_get_settings_tabs();
    $settings_tabs = empty($settings_tabs) ? array() : $settings_tabs;
    $active_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'general';
    $active_tab = array_key_exists($active_tab, $settings_tabs) ? $active_tab : 'general';
    $sections = edd_get_settings_tab_sections($active_tab);
    $key = 'main';
    if (is_array($sections)) {
        $key = key($sections);
    }
    $registered_sections = edd_get_settings_tab_sections($active_tab);
    $section = isset($_GET['section']) && !empty($registered_sections) && array_key_exists($_GET['section'], $registered_sections) ? sanitize_text_field($_GET['section']) : $key;
    // Unset 'main' if it's empty and default to the first non-empty if it's the chosen section
    $all_settings = edd_get_registered_settings();
    // Let's verify we have a 'main' section to show
    $has_main_settings = true;
    if (empty($all_settings[$active_tab]['main'])) {
        $has_main_settings = false;
    }
    // Check for old non-sectioned settings (see #4211 and #5171)
    if (!$has_main_settings) {
        foreach ($all_settings[$active_tab] as $sid => $stitle) {
            if (is_string($sid) && is_array($sections) && array_key_exists($sid, $sections)) {
                continue;
            } else {
                $has_main_settings = true;
                break;
            }
        }
    }
    $override = false;
    if (false === $has_main_settings) {
        unset($sections['main']);
        if ('main' === $section) {
            foreach ($sections as $section_key => $section_title) {
                if (!empty($all_settings[$active_tab][$section_key])) {
                    $section = $section_key;
                    $override = true;
                    break;
                }
            }
        }
    }
    ob_start();
    ?>
	<div class="wrap <?php 
    echo 'wrap-' . $active_tab;
    ?>
">
		<h1 class="nav-tab-wrapper">
			<?php 
    foreach (edd_get_settings_tabs() as $tab_id => $tab_name) {
        $tab_url = add_query_arg(array('settings-updated' => false, 'tab' => $tab_id));
        // Remove the section from the tabs so we always end up at the main section
        $tab_url = remove_query_arg('section', $tab_url);
        $active = $active_tab == $tab_id ? ' nav-tab-active' : '';
        echo '<a href="' . esc_url($tab_url) . '" class="nav-tab' . $active . '">';
        echo esc_html($tab_name);
        echo '</a>';
    }
    ?>
		</h1>
		<?php 
    $number_of_sections = count($sections);
    $number = 0;
    if ($number_of_sections > 1) {
        echo '<div><ul class="subsubsub">';
        foreach ($sections as $section_id => $section_name) {
            echo '<li>';
            $number++;
            $tab_url = add_query_arg(array('settings-updated' => false, 'tab' => $active_tab, 'section' => $section_id));
            $class = '';
            if ($section == $section_id) {
                $class = 'current';
            }
            echo '<a class="' . $class . '" href="' . esc_url($tab_url) . '">' . $section_name . '</a>';
            if ($number != $number_of_sections) {
                echo ' | ';
            }
            echo '</li>';
        }
        echo '</ul></div>';
    }
    ?>
		<div id="tab_container">
			<form method="post" action="options.php">
				<table class="form-table">
				<?php 
    settings_fields('edd_settings');
    if ('main' === $section) {
        do_action('edd_settings_tab_top', $active_tab);
    }
    do_action('edd_settings_tab_top_' . $active_tab . '_' . $section);
    do_settings_sections('edd_settings_' . $active_tab . '_' . $section);
    do_action('edd_settings_tab_bottom_' . $active_tab . '_' . $section);
    // For backwards compatibility
    if ('main' === $section) {
        do_action('edd_settings_tab_bottom', $active_tab);
    }
    // If the main section was empty and we overrode the view with the next subsection, prepare the section for saving
    if (true === $override) {
        ?>
<input type="hidden" name="edd_section_override" value="<?php 
        echo $section;
        ?>
" /><?php 
    }
    ?>
				</table>
				<?php 
    submit_button();
    ?>
			</form>
		</div><!-- #tab_container-->
	</div><!-- .wrap -->
	<?php 
    echo ob_get_clean();
}
/**
 * Run the EDD Instsall process
 *
 * @since  2.5
 * @return void
 */
function edd_run_install()
{
    global $wpdb, $edd_options, $wp_version;
    if (!function_exists('edd_create_protection_files')) {
        require_once EDD_PLUGIN_DIR . 'includes/admin/upload-functions.php';
    }
    // Setup the Downloads Custom Post Type
    edd_setup_edd_post_types();
    // Setup the Download Taxonomies
    edd_setup_download_taxonomies();
    // Clear the permalinks
    flush_rewrite_rules(false);
    // Add Upgraded From Option
    $current_version = get_option('edd_version');
    if ($current_version) {
        update_option('edd_version_upgraded_from', $current_version);
    }
    // Setup some default options
    $options = array();
    // Pull options from WP, not EDD's global
    $current_options = get_option('edd_settings', array());
    // Checks if the purchase page option exists
    if (!array_key_exists('purchase_page', $current_options)) {
        // Checkout Page
        $checkout = wp_insert_post(array('post_title' => __('Checkout', 'easy-digital-downloads'), 'post_content' => '[download_checkout]', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'page', 'comment_status' => 'closed'));
        // Purchase Confirmation (Success) Page
        $success = wp_insert_post(array('post_title' => __('Purchase Confirmation', 'easy-digital-downloads'), 'post_content' => __('Thank you for your purchase! [edd_receipt]', 'easy-digital-downloads'), 'post_status' => 'publish', 'post_author' => 1, 'post_parent' => $checkout, 'post_type' => 'page', 'comment_status' => 'closed'));
        // Failed Purchase Page
        $failed = wp_insert_post(array('post_title' => __('Transaction Failed', 'easy-digital-downloads'), 'post_content' => __('Your transaction failed, please try again or contact site support.', 'easy-digital-downloads'), 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'page', 'post_parent' => $checkout, 'comment_status' => 'closed'));
        // Purchase History (History) Page
        $history = wp_insert_post(array('post_title' => __('Purchase History', 'easy-digital-downloads'), 'post_content' => '[purchase_history]', 'post_status' => 'publish', 'post_author' => 1, 'post_type' => 'page', 'post_parent' => $checkout, 'comment_status' => 'closed'));
        // Store our page IDs
        $options['purchase_page'] = $checkout;
        $options['success_page'] = $success;
        $options['failure_page'] = $failed;
        $options['purchase_history_page'] = $history;
    }
    // Populate some default values
    foreach (edd_get_registered_settings() as $tab => $sections) {
        foreach ($sections as $section => $settings) {
            // Check for backwards compatibility
            $tab_sections = edd_get_settings_tab_sections($tab);
            if (!is_array($tab_sections) || !array_key_exists($section, $tab_sections)) {
                $section = 'main';
                $settings = $sections;
            }
            foreach ($settings as $option) {
                if ('checkbox' == $option['type'] && !empty($option['std'])) {
                    $options[$option['id']] = '1';
                }
            }
        }
    }
    $merged_options = array_merge($edd_options, $options);
    $edd_options = $merged_options;
    update_option('edd_settings', $merged_options);
    update_option('edd_version', EDD_VERSION);
    // Create wp-content/uploads/edd/ folder and the .htaccess file
    edd_create_protection_files(true);
    // Create EDD shop roles
    $roles = new EDD_Roles();
    $roles->add_roles();
    $roles->add_caps();
    $api = new EDD_API();
    update_option('edd_default_api_version', 'v' . $api->get_version());
    // Create the customers database
    @EDD()->customers->create_table();
    // Check for PHP Session support, and enable if available
    EDD()->session->use_php_sessions();
    // Add a temporary option to note that EDD pages have been created
    set_transient('_edd_installed', $merged_options, 30);
    if (!$current_version) {
        require_once EDD_PLUGIN_DIR . 'includes/admin/upgrades/upgrade-functions.php';
        // When new upgrade routines are added, mark them as complete on fresh install
        $upgrade_routines = array('upgrade_payment_taxes', 'upgrade_customer_payments_association', 'upgrade_user_api_keys', 'remove_refunded_sale_logs');
        foreach ($upgrade_routines as $upgrade) {
            edd_set_upgrade_complete($upgrade);
        }
    }
    // Bail if activating from network, or bulk
    if (is_network_admin() || isset($_GET['activate-multi'])) {
        return;
    }
    // Add the transient to redirect
    set_transient('_edd_activation_redirect', true, 30);
}