/**
 * Change Downloads Upload Directory
 *
 * Hooks the edd_set_upload_dir filter when appropriate. This function works by
 * hooking on the WordPress Media Uploader and moving the uploading files that
 * are used for EDD to an edd directory under wp-content/uploads/ therefore,
 * the new directory is wp-content/uploads/edd/{year}/{month}. This directory is
 * provides protection to anything uploaded to it.
 *
 * @since 1.0
 * @global $pagenow
 * @return void
 */
function edd_change_downloads_upload_dir()
{
    global $pagenow;
    if (!empty($_REQUEST['post_id']) && ('async-upload.php' == $pagenow || 'media-upload.php' == $pagenow)) {
        if ('download' == get_post_type($_REQUEST['post_id'])) {
            edd_create_protection_files(true);
            add_filter('upload_dir', 'edd_set_upload_dir');
        }
    }
}
/**
 * Misc Settings Sanitization
 *
 * @since 1.6
 * @param array $input The value inputted in the field
 * @return string $input Sanitizied value
 */
function edd_settings_sanitize_misc($input)
{
    global $edd_options;
    if (!current_user_can('manage_shop_settings')) {
        return $input;
    }
    if (edd_get_file_download_method() != $input['download_method'] || !edd_htaccess_exists()) {
        // Force the .htaccess files to be updated if the Download method was changed.
        edd_create_protection_files(true, $input['download_method']);
    }
    if (!empty($input['enable_sequential']) && !edd_get_option('enable_sequential')) {
        // Shows an admin notice about upgrading previous order numbers
        EDD()->session->set('upgrade_sequential', '1');
    }
    return $input;
}
/**
 * Install
 *
 * Runs on plugin install by setting up the post types, custom taxonomies,
 * flushing rewrite rules to initiate the new 'downloads' slug and also
 * creates the plugin and populates the settings fields for those plugin
 * pages. After successful install, the user is redirected to the EDD Welcome
 * screen.
 *
 * @since 1.0
 * @global $wpdb
 * @global $edd_options
 * @global $wp_version
 * @return void
 */
function edd_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();
    // Checks if the purchase page option exists
    if (!edd_get_option('purchase_page', false)) {
        // 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 => $settings) {
        foreach ($settings as $option) {
            if ('checkbox' == $option['type'] && !empty($option['std'])) {
                $options[$option['id']] = '1';
            }
        }
    }
    update_option('edd_settings', array_merge($edd_options, $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', $options, 30);
    // Bail if activating from network, or bulk
    if (is_network_admin() || isset($_GET['activate-multi'])) {
        return;
    }
    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);
        }
    }
    // Add the transient to redirect
    set_transient('_edd_activation_redirect', true, 30);
}
/**
 * Misc File Download Settings Sanitization
 *
 * @since 2.5
 * @param array $input The value inputted in the field
 * @return string $input Sanitizied value
 */
function edd_settings_sanitize_misc_file_downloads($input)
{
    if (!current_user_can('manage_shop_settings')) {
        return $input;
    }
    if (edd_get_file_download_method() != $input['download_method'] || !edd_htaccess_exists()) {
        // Force the .htaccess files to be updated if the Download method was changed.
        edd_create_protection_files(true, $input['download_method']);
    }
    return $input;
}
 /**
  * Save the htaccess rules
  *
  * @access      public
  * @since       1.0.0
  * @global      array $edd_options The EDD options
  * @return      void
  */
 public function save_htaccess()
 {
     global $edd_options;
     // Bail if nonce can't be verified
     if (!wp_verify_nonce($_POST['edd_save_htaccess_nonce'], 'edd_save_htaccess_nonce')) {
         return;
     }
     // Bail if user shouldn't be editing this
     if (!current_user_can('manage_shop_settings')) {
         return;
     }
     // Sanitize the input
     $contents = esc_html($_POST['htaccess_contents']);
     $edd_options['htaccess_rules'] = $contents;
     update_option('edd_settings', $edd_options);
     edd_create_protection_files(true);
 }